2

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

1
  • Could be due to permissions users set when posting to the group? Maybe the user logged in has no access to the specific posts in the group? You can check whether a image is set (for example) before using the value using isset: php.net/manual/en/function.isset.php Commented May 8, 2013 at 15:04

1 Answer 1

1

The problem is that those elements do not always exist, and you have E_STRICT error reporting turned on in PHP (which actually helps you spot issues).

Thus, you need to make sure that all variables are actually set before trying to output them. Also, make sure to access all variables with a $ prefix. That might look like 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 AT : " , $gUpdated_time;

}

Please note that I removed the Comments variable. You'll need a separate loop inside the first one to access all the comments.

Sign up to request clarification or add additional context in comments.

6 Comments

That stopped the error codes, but it doesn't return anything? This is the Feed which shows there are objects
There's a few other issues. Try using for ($i = 0; $i < count($parsed_json->data); $i++) instead of what you have now.
@ServerSideSkittles Have a look at this again. There was a host of problems :) Cool name, btw
Haha thanks :) Also the update is spot on, thankyou so much I have been at this for hours!
@ServerSideSkittles It also took me terribly long to spot the issue with the [i]. I suggest you use a PHP IDE like NetBeans to write your code. It would immediately alert you to mistakes like that, and it's free :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.