将cURL json数组响应转换为关联数组 [英] turn cURL json array response to an associate array

查看:90
本文介绍了将cURL json数组响应转换为关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的cURL请求

I have a cURL request like so

$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

如何将其放入关联数组中?

How can I put this into a Associative array?

我已经尝试过了

json_decode($response,true));

而此

ksort($response);

而此

var_dump($response);

似乎没有任何效果。.

我希望能够这样输出

echo $reponse['Name'];

有什么帮助吗?谢谢

推荐答案

默认情况下,curl_exec将从服务器获取的数据输出到标准输出,因此$ response变量不会真正包含实际的响应数据。如果要获取变量中的数据,请在调用curl_exec之前设置CURLOPT_RETURNTRANSFER选项。

By default curl_exec outputs the data it gets from the server to the standard output so your $response variable doesn't really have the actual response data in it. If you want to get the data in a variable set the CURLOPT_RETURNTRANSFER option before calling curl_exec.

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)

这篇关于将cURL json数组响应转换为关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆