I have a curl setup as follows to access a json object
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sitename.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLO开发者_运维百科PT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookiefilename.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host:sitename.com'));
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
echo $output->property;
I get the error 'trying to access property of non object'.
The only time you are trying to access a property of an object, is here:
echo $output->property;
So do a var_dump($output)
to see what is happening before and after the previous line:
var_dump($output)
$output = json_decode($output);
var_dump($output)
You haven't said what you've tried or looked into.
The most likely thing would seem to be that the site isn't returning valid JSON, and so json_decode
is returning NULL
.
Alternately, perhaps what's being returned is valid JSON, but not an object (e.g., true
or false
, although at the top level a JSON package should always be an object or an array).
精彩评论