Php 使用curl获取json响应的值和键

Php 使用curl获取json响应的值和键,php,json,Php,Json,我使用curl从服务返回json数据。var转储工作正常,但是当我试图访问一个键值对时,我什么也得不到?其中一个键是status,这是我试图在if语句中检索的内容 //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded')); curl_setopt($ch, CURLOPT_USE

我使用curl从服务返回json数据。var转储工作正常,但是当我试图访问一个键值对时,我什么也得不到?其中一个键是status,这是我试图在if语句中检索的内容

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_USERAGENT, '$AgentString');
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
    "Accept: application/json"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$json = json_decode(utf8_encode($result), true);

var_dump($json["return"]);

if (isset($json->status)) {
   // do something
   print("yes");
} else {
print("No");
}

由于
json_decode
中的第二个参数为true,因此结果将作为关联数组返回

$json->status
应该是

$json["status"]

var\u dump($json)
输出什么?此外,您正在
var_dump
ing
$json[“return”]
,但尝试访问
$json->status
。无论是否返回,都会转储json数据的值。所以看起来我删除了一些安全内容:string(1480)“{”response“{”header“{”status“{”SUCCESS“}”,table“{”rows“{”row“{”ssoabled“,”false,“active“:”authKey“:”26facbec-a6e1-401a-86e1-fd4f467598d8“,”authType“:”默认身份验证“,”身份验证“,”null“,}”我知道使用return会停止php中的任何进一步操作,所以我也删除了它以进行测试,但仍然没有得到值。@JamesFerguson:您甚至没有得到“title”的var_转储?没有回答你的问题?我确实有一个标题键对,我的所有字段都显示在var转储中。但是访问它们我什么也得不到。好的,如果我保留我的代码并这样做:if(isset($json[“return”][“status”]))它很有效。你知道我为什么需要返回吗?也许是curl问题,而不是php解码问题?@JamesFerguson:如果在
return
中找到响应(如果其他网站以该格式返回,可能会出现这种情况),那么你就需要访问它。