我有一个类似这样的cURL请求:
$ch = curl_init();
$data = 'filter=year(StartTime)' . urlencode(' eq 2013 and ') .'month(StartTime)'. urlencode(' eq 06') ;
curl_setopt($ch, CURLOPT_URL, "http://url.com/id()/events?$".$data);
$headers = array(
'Accept: application/json',
'Content-type: application/json',
'X-ApiKey : XXXXXXXXXX'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<br><br>
<?php
echo "the name". $result['Name'];
?>
</body>
</html>
这是它所打印的内容。
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 218 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET- WEBSRV X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET - ARR02 Date: Mon, 01 Jul 2013 02:52:31 GMT [{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}]
the name
如何将此内容放入关联数组中?
我尝试了以下代码:
json_decode($response,true));
并且这个
ksort($response);
并且这个
var_dump($response);
但是似乎什么都不起作用...
我希望能够像这样输出
echo $reponse['Name'];
有需要帮忙的吗?谢谢。
var_dump($response);
“不起作用” - 它给出了什么? 你是在努力从CURL调用中获取JSON字符串,还是在努力将其解码为数组? - IMSoP$response = var_dump($response); echo $response['Name'];
,输出结果为bool(true)
。看起来从 CURL 中获取 JSON 字符串没问题,只是无法将其转换为关联数组。 - vinylDeveloperjson_decode($response, true));
对我有用。 - vishalknishad