【发布时间】: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"]是那个数据结构的字符串......