json - Returning empty variables in PHP using Facebook Graph -
i trying retrieve data json feed , display using php. issue elements return empty. have in loop want return each post, not have picture, message or comments , comes notice: undefined variable. (i trying retrieve data fb group)
here settings
<?php // settings $groupid = "231809390188650"; $accesstoken = "mytokengoeshere"; // request , parse json $json_string = file_get_contents("https://graph.facebook.com/$groupid/feed? access_token=$accesstoken"); $parsed_json = json_decode($json_string); ($i = 0; $i < $json_string; $i++) { // returned data $gimage = $parsed_json->data[i]->picture; $gmessage = $parsed_json->data[i]->message; $gcreated_time = $parsed_json->data[i]->created_time; $gupdated_time = $parsed_json->data[i]->updated_time; $gcomments_message = $parsed_json->data[i]->comments->data[i]->message; }; ?>
here output (rough example)
<h1> facebook group </h1> <?php echo "image url : " , $gimage; echo "message : " , $gmessage; echo "date posted : " , $gcreated_time; echo "updated @ : " , $gupdated_time; echo "comments: ", $gcomments_message; ?>
with notice: undefined variable: gimage etc each one. i'm not sure why happening. here example of json feed shows not elements have in them. http://pastebin.com/eujce5vt
the problem elements not exist, , have e_strict error reporting turned on in php (which helps spot issues).
thus, need make sure variables set before trying output them. also, make sure access variables with $
prefix. might this:
for ($i = 0; $i < $json_string; $i++) { $gimage = $parsed_json->data[$i]->picture; $gmessage = $parsed_json->data[$i]->message; $gcreated_time = $parsed_json->data[$i]->created_time; $gupdated_time = $parsed_json->data[$i]->updated_time; if (isset($gimage)) echo "image url : " , $gimage; if (isset($gmessage)) echo "message : " , $gmessage; if (isset($gcreated_time)) echo "date posted : " , $gcreated_time; if (isset($gupdated_time)) echo "updated @ : " , $gupdated_time; }
please note removed comments variable. you'll need separate loop inside first 1 access comments.
Comments
Post a Comment