7

I am trying to use php to parse a JSON feed of posts using Facebook Graph API

I found the following solution for comments...

<?php 

$request_url ="https://graph.facebook.com/comments/?

ids=http://www.youtube.com/watch?v=fyF-fj-1coY&feature=player_embedded";
$requests = file_get_contents($request_url);

$fb_response = json_decode($requests);




foreach ($fb_response as $key => $response) {
  foreach ($fb_response->$key as $data) {
    foreach ($data as $item) {
      echo 'NAME: ' . $item->name . '<br />';
      echo 'From ID: ' . $item->from->id . '<br />';
      echo 'From Name: ' . $item->from->name . '<br />';
      echo 'Message: ' . $item->message . '<br />';
      echo 'Timestamp: ' . $item->created_time . '<br /><br />';
    }
  }
} 
    ?>

This is the url id I'm working with: https://graph.facebook.com/210849652406/feed/?access_token={VALID_USER_TOKEN}

I just don't know how to call the items for this feed. I'm trying to make the comments parse with this post/feed but I get essentially nothing. I want the basic items like name of the post, caption, etc. I think if I just could get the name of the post I could figure it all out!

2
  • be careful posting usable access_tokens on the web, someone might use it for nefarious purposes. I edited it out for you Commented Feb 3, 2012 at 19:49
  • It's actually from facebook testing so it expires after a while, but that might throw people off anyway, so thank you! Commented Feb 3, 2012 at 21:27

3 Answers 3

7

You are looping incorrectly

try this

foreach($fb_response->data as $item){
echo 'Message: ' . $item->message . '<br />';//there is no name returned on a comment
echo 'From ID: ' . $item->from->id . '<br />';
 echo 'From Name: ' . $item->from->name . '<br />';
 echo 'Message: ' . $item->message . '<br />';
 echo 'Timestamp: ' . $item->created_time . '<br /><br />';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Do you have warnings/errors displayed? Ensure that you have extension=php_openssl.dll (or .so) enabled in your php.ini or you will get no results. This is because you are fetching from a secure site.

Also $item->name is an undefined property in the JSON. Perhaps you mean $item->id. Everything else looks ok.

2 Comments

The code I'm shown does work, however I'm trying to get that code to work for displaying posts or the feed from facebook not the comments from a specific post. The graph url that I'm trying to parse is this: graph.facebook.com/210849652406/feed + a valid access key
I found the answer here: stackoverflow.com/questions/4582535/… Thanks for your responses.
0

Why aren't you using the PHP SDK?

https://developers.facebook.com/docs/reference/php/

Comments

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.