Facebook Page JSON & RSS Feed

Ever wanted to put a Facebook page’s feed into your website? Every Facebook page has an RSS & JSON feed. In this tutorial I will show you how to use PHP and JSON to pull the feed from Facebook without authentication.

  1. You will need your page ID. You can get this from https://graph.facebook.com/yourpage for example https://graph.facebook.com/HoosierHeights If your page does not have a username, you can visit your page and copy the last numbers in the URL for example:

    https://www.facebook.com/pages/HoosierHeights/163276271689 

  2. Now that you have your page ID you can view the JSON feed at this URL:
    http://www.facebook.com/feeds/page.php?id=163276271689&format=json
    (replacing the number with your page ID).
  3. Here is the PHP code to pull the JSON feed into your site. (Be sure to replace the URL with your own).
    <? 
    //replace the Page ID with your own
    $url = "http://www.facebook.com/feeds/page.php?id=163276271689&format=json"; 
    
    // disguises the curl using fake headers and a fake user agent. 
    function disguise_curl($url) 
    { 
      $curl = curl_init(); 
    
      // Setup headers - the same headers from Firefox version 2.0.0.6 
      // below was split up because the line was too long. 
      $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
      $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
      $header[] = "Cache-Control: max-age=0"; 
      $header[] = "Connection: keep-alive"; 
      $header[] = "Keep-Alive: 300"; 
      $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
      $header[] = "Accept-Language: en-us,en;q=0.5"; 
      $header[] = "Pragma: "; // browsers keep this blank. 
    
      curl_setopt($curl, CURLOPT_URL, $url); 
      curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla'); 
      curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
      curl_setopt($curl, CURLOPT_REFERER, ''); 
      curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
      curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
    
      $html = curl_exec($curl); // execute the curl command 
      curl_close($curl); // close the connection 
    
      return $html; // and finally, return $html 
    } 
    
    // uses the function and displays the text off the website 
    $text = disguise_curl($url); 
    
    $json_feed_object = json_decode($text);
    
    foreach ( $json_feed_object->entries as $entry )
    {
            echo "<h2>{$entry->title}</h2>";
    	$published = date("g:i A F j, Y", strtotime($entry->published));
    	echo "<small>{$published}</small>";
    	echo "<p>{$entry->content}</p>";
    	echo "<hr />";
    }
    ?>
    

That’s it! Easy right? I would like to give credit to @mdlamar for providing help with this idea.

18 comments

  1. Milo says:

    Note that you need to remove the ‘s’ from ‘https’ in your URL. That’s a little gotcha that I even forgot about even though I encountered it once before. Alternatively, you can tweak your CURLOPTs to probably work with HTTPS. See http://www.php.net/manual/en/curl.constants.php for more details on CURLOPTs

  2. Kef says:

    Thank you. This rocks and saved me a lot of time.

  3. Holy Cats-in-pajamas!! That’s awesome… Thanks so much for this gem.

    I made a slight mod to make this a callable function using the facebook ID and included a limit on the number of items posted.

    Well done!

    • Mel says:

      Hi Martin, I am not good with PHP, can you tell me how you can:

      1- Limit the number of items posted
      2- Limit the amount of descriptive text below the title
      3- Link the titles to FB page

  4. Eric says:

    How come the like data, comment data, and other fields are blank even though for some posts for some pages there is data for those fields?

    Have you ever seen the json feed provide that type of data for each post on a page?

    • Milo says:

      Hi Eric,

      I can’t say for certain without an example. As a wild guess, I’d say the Graph is not dynamically generated. That is, it may be behind the actual post by a minute, hour, or day. Maybe you can link us an example?

  5. John Woods says:

    Could someone help me find the RSS feed. I’d like to use it to post facebook page updates to my Hello Bar.

  6. Very nice post man… i was looking for Atom Feed or RSS but this was very informative also… So should i just put this php code into my page and it will appear as link or some kind of button/banner? Or i am just not understanding this whole thing? :O
    Peace,
    Stefan Zdravevski

  7. pdubau says:

    LOVE the script. Does anybody know how to limit the number of posts shown? Would be greatly appreciated :)

  8. frankie says:

    did you notice that the received feeds are limited with “…” at the end of each feed if its too long?do you have any idea how would i fix this to show the complete feed

  9. Thanks for your article!! Just a question: The links in the feed are broken. Do you have any direction to fix the broken links

  10. Milo says:

    They may have changed some things. They always are doing so. If you want to limit the number of posts you could count the number returned and use a for loop instead of a foreach loop.

    $entries = $json_feed_object->entries;
    $numEntries = count($entries);
    //here you can start and end $i at any number to limit entries
    for ($i = 0; $i < $numEntries; $i++) {
    echo "{$entries[$i]->title}";
    $published = date("g:i A F j, Y", strtotime($entries[$i]->published));
    echo "{$published}";
    echo "{$entries[$i]->content}";
    echo "";
    }

    As for links being broken, I have no idea what you mean. What do they look like?

  11. Macsam says:

    Hi, thank you so much for posting this code. :-)
    Awesome snippet.

    Short question…i am trying to embed the posted images but unfortunately this code doesnt work:

    echo “alternate}\” />”;

    Any idea?

    Thank you for responding.,

    Sam

  12. Milo says:

    bump. Thanks again for posting this Josh. I come back when I don’t want to have to rewrite it :)

Leave a Reply

Your email address will not be published. Required fields are marked *