I am trying to retrieve data from a json feed and display using PHP. Only issue is that some elements will return back empty. As I have this in a loop I want to return each post, however not all have a picture, message or comments and comes back as Notice: Undefined variable. (I am trying to retrieve data from a fb group)
Here are the settings
<?php
// Settings
$groupID = "231809390188650";
$accessToken = "myTokenGoesHere";
// Request and parse json
$json_string = file_get_contents("https://graph.facebook.com/$groupID/feed? access_token=$accessToken");
$parsed_json = json_decode($json_string);
for ($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 is the output (rough example)
<h1> Facebook Group </h1>
<?php
echo "IMAGE URL : " , $gImage;
echo "MESSAGE : " , $gMessage;
echo "DATE POSTED : " , $gCreated_time;
echo "UPDATED AT : " , $gUpdated_time;
echo "COMMENTS: ", $gComments_message;
?>
With this I get Notice: Undefined variable: gImage etc for each one. I'm not sure why this is happening. Here is an example of the json feed which shows that not all elements will have something in them. http://pastebin.com/eUJce5VT