Reading a Facebook Page RSS Feed with PHP
I don’t normally write specific “tutorial” type posts like this on my blog, but during a recent project I had to add functionality that read feeds from a variety of sources, including RSS, Twitter, Youtube, and Facebook. If you’ve ever read any type of XML feed via PHP reading most of these feeds are easy, and there’s nothing new to it. (If not, it’s pretty simple and there are tons of tutorials online.) However, when I got to the Facebook feed, I ran into endless trouble.
Simply plugging in a Facebook update feed (from a page) to my normal feed reading script didn’t work. Looking at the source of the feed, it was standard XML, and no different from reading any other type of XML. I looked closer into the Facebook-specific feed tags, altered my code time after time, and to no avail.
Ultimately, in order to move the project along I had to find a quick solution that wasn’t the most user friendly. I read a tutorial somewhere on reading Facebook feeds where if you burned your feed via Feedburner, and then used that URL instead, it worked just fine. I tried it, it worked, and I had to make my users enter in their own Facebook feed to Feedburner, then find that URL, and then plug that URL into the app so it could read the feed. Like I said, not very user friendly at all!
After a long break, and a fresh look at things weeks later however, I finally figured out a solution! For anyone else who may ever be looking for the same solution for whatever reason, below is the code I used, with comments for explanation:
<?php
// Without this "ini_set" Facebook's RSS url is all screwy for reading!
// This is the most essential line, don't forget it.
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
// This URL is the URL to the Facebook Page's RSS feed.
// Go to the page's profile, and on the left-hand side click "Get Updates vis RSS"
$rssUrl = "http://www.facebook.com/feeds/page.php?id=119425148134433&format=rss20";
$xml = simplexml_load_file($rssUrl); // Load the XML file
// This creates an array called "entry" that puts each <item> in FB's
// XML format into the array
$entry = $xml->channel->item;
// This is just a blank string I create to add to as I loop through our
// FB feed. Feel free to format however you want, or do whatever else
// you want with the data.
$returnMarkup = '';
// Now we'll loop through are array. I just have it going up to 3 items
// for this example.
for ($i = 0; $i < 3; $i++) {
$returnMarkup .= "<h3>".$entry[$i]->title."</h3>"; // Title of the update
$returnMarkup .= "<p>".$entry[$i]->link."</p>"; // Link to the update
$returnMarkup .= "<p>".$entry[$i]->description."</p>"; // Full content
$returnMarkup .= "<p>".$entry[$i]->pubDate."</p>"; // The date published
$returnMarkup .= "<p>".$entry[$i]->author."</p>"; // The author (Page Title)
}
// Finally, we return (or in this case echo) our formatted string with our
// Facebook page feed data in it!
echo $returnMarkup;
?>
Needless to say, I’m rebuilding the Facebook feed portion of the project I’m working on — and fortunately for it’s future users I figured this out before it’s launch!