← Back to blog home

Easy way to load Facebook news feed through RSS / atom10

05 Jun

Until recently you could import your facebook news feed using a json feed available through the Facebook Graph API without needing to set up an oAuth token. Unfortunately, facebook pulled the rug out from under us all when they started requiring oAuth tokens on the basic graph API. This caused many sites to start receiving the following error: "OAuthException An access token is required to request this resource." Pretty annoying. I was ready to jump through facebook's hoops and create a facebook app dedicated to providing access to the news feed when I realized each page has an rss feed built right in. IMO it's not AS good as the data from the graph API but for most situations it will get the job done.

 

Considerations

In testing this I realized that there are some facebook fan pages that for whatever reason DO NOT have an rss feed. I work on several sites that have facebook fan pages and unfortunately a few of them just return an empty string. I have no idea why or how to fix this so don't ask. If you figure it out please share. If you find yourself in this situation you have to use the Facebook Graph API. You can use this guide: Get latest status from facebook using Graph API

 

Overview

The process is fairly simple we just load the fan page rss feed using the facebook feeds url as follows: http://www.facebook.com/feeds/page.php?id=YOUR_FACEBOOK_ID&format=atom10

One thing I noticed is facebook doesn't allow basic curl calls to this URL. For some reason it won't return any data unless you set the user agent to pretend to be one of the major browsers. I assume this is a very lame attempt to keep websites and bots from pulling data from their feeds. So in my example I use a basic Firefox / windows user agent.

 

How do I get my page user id?

It's very simple just go to your facebook fan page and click your profile picture in the top left. Once you do you'll be taken to a URL that looks like this: http://www.facebook.com/media/set/?set=pa.#########

after "pa." where the ##### is will be your user id.

You can also go to your photo albums and you'll get a url like this: https://www.facebook.com/photos.php?id=####### … again where the ##### is where your facebook id will be.

 

The Nuts and Bolts

Here is a quick PHP example for pulling these feeds. If you're using this on a production site I HIGHLY advise pulling this in to a database using a cron job then loading the data to the site from the database. That way you're not hitting facebooks server every page load AND if (when) facebook changes things again you won't get errors on the public facing page.

//function to retrieve posts from facebook server
function loadFB($fbID){
	//facebook feed url
    $url="http://www.facebook.com/feeds/page.php?id=".$fbID."&format=atom10";
    
    //load and setup CURL
    $c = curl_init();
    
    //set options and make it up to look like firefox
	$userAgent = "Firefox (WindowsXP) - Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
	curl_setopt($c, CURLOPT_USERAGENT, $userAgent);
	curl_setopt($c, CURLOPT_URL,$url);
	curl_setopt($c, CURLOPT_FAILONERROR, true);
	curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($c, CURLOPT_AUTOREFERER, true);
	curl_setopt($c, CURLOPT_RETURNTRANSFER,true);
	curl_setopt($c, CURLOPT_VERBOSE, false);     
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    
    //get data from facebook and decode XML
    $page = curl_exec($c);
    $pxml= new SimpleXMLElement($page);

    //close the connection
    curl_close($c);
     
    //return the data as an object
    return $pxml->entry;
}

//BE SURE to enter your facebook id here
$fbid="FACEBOOK_ID_GOES_HERE";

//how may posts to show
$fbLimit=10;
//variable used to count how many weÕve loaded
$fbCount=0;

//call the function and get the posts from facebook
$myPosts=loadFB($fbid);

//set timezone (change this to your timezone)
date_default_timezone_set("America/Chicago");

//loop through all the posts we got from facebook
foreach($myPosts as $dPost){
      //get the post date / time and convert to unix time
      $dTime = strtotime($dPost->published);
      //format the date / time into something human readable
      //if you want it formatted differently look up the php date function
      $myTime=date("M d Y h:ia",$dTime);
      //output the date / time
      echo("<b>".$myTime."</b>");
      //output the message body
      echo($dPost->content);
      //add a line break to separate comments
      echo("<br /><br />");    
      //increment counter
      $fbCount++;
      //if we've outputted the number set above in fblimit we're done
      if($fbCount >= $fbLimit) break;    
}

This code is fairly well commented and should serve as a good start to be able to easily pull in your facebook news feed through rss to any website.

 
14 Comments

Posted in Guides

 

Tags: , , , , , ,

Leave a Reply

Notify me of future comments

(I don't spam or share your e-mail. Unsubscribing is as easy as clicking the "unsubscribe" link in the notifications)
 

 

 
  1. spiffomatic64

    June 6, 2011 at 1:39 pm

    This does not appear to be working? I get the following message “This feed URL is no longer valid. Visit this page to find the new URL, if you have access, http://www.facebook.com/profile.php?id=

     
  2. illebas

    June 8, 2011 at 4:17 pm

    AWESOME!!! Works perfectly. Thank you!

     
  3. Victor

    June 9, 2011 at 9:19 pm

    yea this doesnt seems to work anymore ..thx

     
  4. Lenny

    June 10, 2011 at 11:46 am

    As I mention in the post, for some unknown reason some facebook pages have RSS feeds and some don’t. I really don’t know why. In a test about half of my clients facebook pages actually had RSS feeds enabled and half didn’t. If your page doesn’t have RSS you can use the graph API.

     
  5. Daniel

    June 21, 2011 at 12:10 am

    This no longer works

     
  6. Ivan

    July 21, 2011 at 4:13 pm

    Thank you! You saved me:)

     
  7. phoenix webber

    July 23, 2011 at 2:05 am

    ok, so, by “easily” you actually mean “first learn how to do complex coding in coputer-speak, then learn how to integrate that with browsers, then follow the detailed instructions on this webpage”….

    isn’t there an add-on/extension for this?

     
  8. Lenny

    September 21, 2011 at 12:58 pm

    Actually, I meant “easily” for a web developer… Even a newbie developer who has basic PHP skills should be able to use this tutorial… if you’re making websites and don’t know how to code this site isn’t for you.

     
  9. Lenny

    September 21, 2011 at 1:08 pm

    true story.

     
  10. phoenix webber

    September 21, 2011 at 3:58 pm

    ah, i see. no worries. the extent of my developing is only HTML/CSS, so way out of my league!

     
  11. Freehill Media

    February 6, 2012 at 10:38 pm

    Whether or not your RSS is available will probably depend on your privacy settings. (just a thought)

     
  12. Adam Walter

    June 12, 2012 at 10:45 am

    This is awesome, thank you! Do you know how to further segment out the content? For example, this setup makes it easy to style the date independently, but all the rest of the content is output as one big blob of text and links, making it hard to style.

     
  13. Pete

    November 8, 2012 at 4:41 am

    Great post. Thanks for this. I was looking for a really simple way of grabbing a users posts and sticking them in my database.

    This works perfectly for me.

    I’m sure there will come a time when they stop this functionality (like Twitter) but for now it works great.

     
  14. Ben

    May 2, 2013 at 12:51 pm

    So far I can’t seem to get ($dPost->link) to return anything other than an empty result. Everything else works as it should.

    Any ideas?