I want to make a PHP JSON data foreach, but I met some problem.
This is my php code:
$ch = curl_init();//use $json = file_get_contents($url); it will return:file_get_contents() [function.file-get-contents]: SSL: fatal protocol error in ...
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body);//if add true, all the resalt is empty.
foreach ($data as $result) {
echo '<div class="title"><a href="'.htmlspecialchars($result->link).'">'.htmlspecialchars($result->message).'<br />'.htmlspecialchars($result->description).'<br />'.htmlspecialchars($result->caption).'</a><br />';
if(!empty($result->attachment->properties[0]->text)){
foreach ($result->attachment->properties[0] as $properties) {
echo htmlspecialchars($properties->name).'<br /><a href="'.htmlspecialchars($properties->href).'">'.htmlspecialchars($properties->text).'</a></div>';
}
}
if(!empty($result->attachment->media)){
echo '<div class="image"><a href="'.htmlspecialchars($result->attachment->media[0]->href).'"><img src="'.htmlspecialchars($result->attachment->media[0]->src).'" /><br>'.htmlspecialchars($result->attachment->media[0]->type).'</a></div>';
}
}
I make a var_dump and get the data return, I post it in the javascript part in http://jsfiddle.net/T375A/. remove outer string(47434)“” and copy to [URL=“http://www.jsonlint.com/”]http://www.jsonlint.com/, can see a json tree clearly.
First: I can not get the properties
part. the return is empty.
Second: it alawys show wrong in line: echo '<div class="image">...
Fatal error: Cannot use object of type stdClass as array in …
How to foreach in a correct way? Thanks.