I am using Curl
to perform a GET request on a Sage server. The response is in JSON format, but I am unable to access the key/values.
An example of the response is below:
{
"$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 */
}
I need to access 2 key/value pairs for each child of $resources
. The first is $uuid
and the second is reference
.
I have attempted various methods including:
$result=curl_exec($ch);
$resources = $result->{'$resources'};
print_r($resources); /* Non-object error */
Can someone shed some light on how I can access these key/values, please?
Update
If I perform the following action, I receive a Notice: Trying to get property of non-object
error.
$result = json_decode(curl_exec($ch));
$resources = $result->{'$resources'};
print_r($resources);
Edit 2
Entire code currently used:
<?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);
?>
Edit 3
Output of 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)