破解php的post的json加密,在PHP中解析JSON响应-受保护的密钥?

我正在使用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,第二个是引用.

我尝试了多种方法,包括:

$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

当前使用的整个代码:

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);

?>

编辑3

var_dump($result);的输出

string '{

"$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": "",

'... (length=5333303)

解决方法:

更新:

服务器响应是使用BOM(字节顺序标记)编码的UTF-8,这导致json_encode失败并显示JSON_ERROR_SYNTAX

工作代码

$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;

}

假设$result是根据最新编辑的json_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,php

来源: https://codeday.me/bug/20191119/2039252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值