在PHP保护的密钥中解析JSON响应?
我正在使用在PHP保护的密钥中解析JSON响应?,php,json,Php,Json,我正在使用Curl在Sage服务器上执行GET请求。响应是JSON格式的,但我无法访问键/值 下面是一个响应示例: { "$descriptor": "Sage Accounts 50 | tradingAccount.", "$totalResults": 1508, "$startIndex": 1, "$itemsPerPage": 1508, "$resource
Curl
在Sage服务器上执行GET请求。响应是JSON格式的,但我无法访问键/值
下面是一个响应示例:
{
"$descriptor": "Sage Accounts 50 | tradingAccount.",
"$totalResults": 1508,
"$startIndex": 1,
"$itemsPerPage": 1508,
"$resources": [
{
"$url": "http://it1:5493/sdata/accounts50/GCRM/{53C58AA8-1677-46CE-BCBE-4F07FED3668F}/tradingAccountCustomer(9a7a0179-85cb-4b65-9d02-73387073ac83)?format=atomentry",
"$uuid": "9a7a0179-85cb-4b65-9d02-73387073ac83",
"$httpStatus": "OK",
"$descriptor": "",
"active": true,
"customerSupplierFlag": "Customer",
"companyPersonFlag": "Company",
"invoiceTradingAccount": null,
"openedDate": "\/Date(1246834800000+0100)\/",
"reference": "1STCL001",
"reference2": null,
"status": "Open"
}
/* Additional results omitted for simplicity */
}
我需要为$resources
的每个子级访问2个键/值对。第一个是$uuid
,第二个是reference
我尝试了各种方法,包括:
$result=curl_exec($ch);
$resources = $result->{'$resources'};
print_r($resources); /* Non-object error */
有人能告诉我如何访问这些键/值吗
更新
如果执行以下操作,我将收到一个通知:尝试获取非对象的属性错误
$result = json_decode(curl_exec($ch));
$resources = $result->{'$resources'};
print_r($resources);
编辑2
当前使用的完整代码:
<?php
header('content-type:application/json');
error_reporting(E_ALL);
$url = "http://it1:5493/sdata/accounts50/GCRM/-/tradingAccounts?format=json";
$header = array();
$header[] = 'Authorization: Basic bWFuYWdlcjpjYmwyMDA4';
$header[] = 'Content-Type: application/json;';
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Set the header
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
// Execute
$result = json_decode(curl_exec($ch));
if ($result === false)
{
// throw new Exception('Curl error: ' . curl_error($crl));
print_r('Curl error: ' . curl_error($ch));
}
// Closing
curl_close($ch);
// Access property $resources
$resources = $result->{'$resources'};
// Dump results
print_r($resources);
?>
你错过了json_解码呼叫。试试这个:
$result = json_decode(curl_exec($ch));
$resources = $result->{'$resources'};
您必须对JSON进行解码这里所有人似乎都缺少的关键事实是,$resources
是JSON中定义的数组,而不是对象,因此JSON_decode()
将它转换为PHP数组,而不是PHP对象
$result = json_decode(curl_exec($ch));
$resources = $result['$resources']; //resources is an array, not an object.
foreach ($resources as $resource) {
//but each resource is an object...
print $resource->{'$url}."\n";
print $resource->{'$uuid}."\n";
// ...etc...
}
更新:
服务器响应是使用BOM(字节顺序标记)进行UTF-8编码的,这导致json\u编码
失败,语法为json\u错误
工作代码
$string = curl_exec($ch);
$object = json_decode(remove_utf8_bom($string),true);
foreach ($object as $key => $value)
if (is_array($value))
foreach($value as $k=>$arr){
print $arr['$uuid'] . PHP_EOL;
print $arr['reference'] . PHP_EOL;
}
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
从此处删除\u utf8\u bom
功能
假设$result
是您最新编辑的json\u decode
的内容,这就是访问键/值的方式
foreach ($result->{'$resources'} as $obj){
print $obj->{'$uuid'} . PHP_EOL;
print $obj->reference . PHP_EOL;
}
// prints out
// 9a7a0179-85cb-4b65-9d02-73387073ac83
// 1STCL001
服务器返回的JSON编码为UTF-8,带有BOM,在字符串的开头放3个字符。只要尝试获取正确编码的JSON,或者如果不能,请删除前3个字符,然后使用JSON\u decode
获取PHP对象。$resources是一个数组,而不是一个对象,因为它以[
您会遇到什么错误?您是否在$ch
句柄中设置了CURLOPT\u RETURNTRANSFER
?@Peter它与此无关。问题是curl\u exec
不解析JSON,@SverriM.Olsen请查看我的编辑curl\u exec
默认返回true
或false
。如果您使用tCURLOPT\u RETURNTRANSFER
则它可能返回一个对象,但也可能返回false
。在对其执行任何操作之前,您应该检查curl\u exec
返回的内容。这是您的问题。这会返回以下错误:注意:在第32行的C:\wamp\www\sage-test.php中尝试获取非对象的属性。这与$resources=$result->{'$resources'};
仔细检查您从远程服务器获得的内容。只需回显curl\u exec返回的内容。我现在收到一条警告:为foreach()提供的参数无效。
error在foreach()之前添加var\u dump
或print\r
查看发生了什么。好的一面是,至少代码通过了$resource=$result['$resources']
line,所以这么多似乎都奏效了。这是我回答的要点。谢谢Peter。进一步调查后,发现Sage服务器确实返回了带有BOM编码的UTF-8。快速substr($result,3)
解决了这个问题。
$result = json_decode(curl_exec($ch));
$resources = $result['$resources']; //resources is an array, not an object.
foreach ($resources as $resource) {
//but each resource is an object...
print $resource->{'$url}."\n";
print $resource->{'$uuid}."\n";
// ...etc...
}
$string = curl_exec($ch);
$object = json_decode(remove_utf8_bom($string),true);
foreach ($object as $key => $value)
if (is_array($value))
foreach($value as $k=>$arr){
print $arr['$uuid'] . PHP_EOL;
print $arr['reference'] . PHP_EOL;
}
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
foreach ($result->{'$resources'} as $obj){
print $obj->{'$uuid'} . PHP_EOL;
print $obj->reference . PHP_EOL;
}
// prints out
// 9a7a0179-85cb-4b65-9d02-73387073ac83
// 1STCL001