首先,感謝您的閱讀。這裏是我的代碼顯示使用php curl檢索json數據的問題
什麼的json數據看起來使用的URL在瀏覽器中查看時一樣,
{
"$totalResults": 1500,
"$startIndex": 1,
"$itemsPerPage": 3,
"$resources": [
{
"$uuid": "5e7b9312-52e5-4fe1-b3e4-633ca04c9764",
"$httpStatus": "OK",
"$descriptor": "",
"name": "Heinz Tomato Sauce Sachets Qty 200"
},
{
"$uuid": "1a0f9dca-c417-4cff-94d2-99d16438723f",
"$httpStatus": "OK",
"$descriptor": "",
"name": "Heinz Brown Sauce Sachet Qty 200"
},
{
"$uuid": "126fdb17-81ce-41b4-bdba-91d0a170262a",
"$httpStatus": "OK",
"$descriptor": "",
"name": "Heinz English Mustard Sachets Qty 300"
}
]
}
test2.php
<?php
// SAGE URL
$url = 'http://localhost:5493/sdata/accounts50/GCRM/-/commodities?select=name&count=3&format=json';
//SAGE USERNAME & PASSWORD
$username = 'test';
$password = 'test';
//initiaize
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute
$result = curl_exec($ch);
//close curl session/free resources
curl_close($ch);
$json = json_decode($result);
foreach ($json->{'$resources'} as $mydata) {
echo $mydata->name;
};
?>
錯誤即時得到
Notice: Trying to get property of non-object in C:\xampp\htdocs\Sage Json\test2.php on line 29
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Sage Json\test2.php on line 29
我不確定發生了什麼問題,如果我從文件中獲得相同的數據,但是如果我從資源中獲取相同的數據牛逼的服務器,如果有人可以提供一些線索我將不勝感激
檢查$ json和$ result變量中的內容。 – mohit
不確定要使用什麼,我做了一個ver_dump(isset());和&結果裏面有東西,但$ json不知道怎麼回事。 – Ajking11