【问题标题】:Extracting values from JSON response with PHP使用 PHP 从 JSON 响应中提取值
【发布时间】:2017-08-08 12:48:35
【问题描述】:

我将 JSON 数据发送到一个 URL,然后我也收到了 JSON。
我正在使用 PHP CURL 接收数据并将其显示在我的页面上。
我的问题是我只能完全获取 json,并且不能仅显示该 JSON 数据中的选定值。
我已经收到了:

//API Url
$url = "https://auth.unilinxx.com/test";

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array("value" => "Gideon lol");

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 

//Execute the request
$result = curl_exec($ch);

$values = json_decode($result, true);

echo $values["value"];

我的结果是:{"value":"U heeft Gideon lol gestuurd."}

我只喜欢这个:U heeft Gideon lol gestuurd.

我该怎么做?

【问题讨论】:

  • 我已经使用了,正如您在我的代码中看到的那样!
  • 您是否正在查看正在输出的 JSON 字符串“auth.unilinxx.com/test”以帮助您了解这里发生了什么?看起来您的 API 正在返回 "value"{"value":{"U heeft... 的嵌套 JSON 数组
  • 除非你在 json 中有 json,否则这不是正确的结果
  • echo $values["value"]; 如果你的输出是{"value":"U heeft Gideon lol gestuurd."} 会失败所以有些东西告诉我这段代码不是正在运行的或者$values["value"] 是那个数据结构的字符串......

标签: php json curl


【解决方案1】:

你的结果变量是返回字符串吗?如果它只返回 TRUE 或 FALSE 请尝试添加此代码

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

【讨论】:

    【解决方案2】:

    您的整个代码是正确的,您只需在代码中将CURLOPT_RETURNTRANSFER 添加到true。其他明智的结果不会存储在变量中。并直接输出到屏幕

    <?php
    //API Url
    $url = "https://auth.unilinxx.com/test";
    
    //Initiate cURL.
    $ch = curl_init($url);
    
    //The JSON data.
    $jsonData = array("value" => "Gideon lol");
    
    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);
    
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //<---------- Add this line
    //Execute the request
    $result = curl_exec($ch);
    
    $values = json_decode($result, true);
    
    echo $values["value"];
    

    【讨论】:

    • 我对ajax请求做同样的事情,当我只是回显$results时它给出了json字符串但是当我添加$values = json_decode($result, true);时jquery console.log返回null,这是为什么?跨度>
    【解决方案3】:
    <?php
    //API Url
    $url = "https://auth.unilinxx.com/test";
    
    //Initiate cURL.
    $ch = curl_init($url);
    
    //The JSON data.
    $jsonData = array("value" => "Gideon lol");
    
    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);
    
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    
    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //<----- I will add this line
    //Execute the request
    $result = curl_exec($ch);
    
    $values = json_decode($result, true);
    

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多