fb

I’ve only really had to do this myself a couple of times and each time I’m asked I’m always left thinking, now, where do I begin? For one thing getting the Facebook page’s RSS feed URL is hard enough, so I thought I’d write a little post on how to get that URL, parse it using PHP and then make it look all fancy with CSS.

First things first, getting the Facebook page’s RSS feed URL. Log into your Facebook account and then head over to the page that you want to turn into a feed. Click on the ‘Notes’ tab and then in the left hand column at the bottom there should be a ‘Subscribe’ section with a link to the RSS feed for the page’s notes. Clicking this, should give you a URL looking a little like the one below:

Facebook notes RSS URL
http://www.facebook.com/feeds/notes.php?id=XXX&viewer=XXX&key=XXX&format=rss20

Now all you need to do is change ‘notes’ in that URL to ‘page’ and you will have a full blown RSS feed of your Facebook page. I’m not sure exactly why this URL isn’t very accessible, perhaps someone can shed some light on the matter?

Facebook page RSS URL
http://www.facebook.com/feeds/page.php?id=XXX&viewer=XXX&key=XXX&format=rss20

This isn’t the final stage in the URL fetching process though. The PHP method I wanted to use (which I’ll come onto in just a few minutes) to parse this feed didn’t like it one bit. So after a bit of mucking around I thought about using FeedBurner to verify the feed and parse the new FeedBurner URL, and what do you know it worked!

So head on over to FeedBurner and burn that Facebook page RSS URL into a more friendly looking FeedBurner URL and you should be all set.

Onto the PHP

First lets set up a few things.

$feed_burner_url = 'http://feeds.feedburner.com/FacebookPage';
 
$doc = new DOMDocument();
$doc->load($feed_burner_url);
 
$feeds = array();
 
$limit = 2;
$counter = 0;

First we put our new fangled RSS feed URL into a variable. Load in the feed using PHP’s DOMDocument class, declare an array to store our feed into, a limit for the amount of posts you want to display (minus one, in actual fact I want to display 3 but the counter beneath is starting at 0) and a counter, to see how what position we are in the loop we are about to write.

Building the RSS feed into the $feeds array

Now I’m going to go through each item in the RSS feed and stick it into our feeds array.

foreach ($doc->getElementsByTagName('item') as $node) {
 
	if	($counter <= $limit)
	{
	$items = array (
			'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
			'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
			'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
			'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
		);
 
	array_push($feeds, $items);
	}
	$counter++;
}

You’ll see above that we’re building up the keys in the array with values from the RSS feed. The key part you need to notice is here: getElementsByTagName('pubDate')

This is the name of the node in the RSS feed, if you view the source of original RSS feed, you should be able to see all the different nodes available for you to use but here they are for you anyway: title, pubDate, author, link and description.

Actually displaying the feed

Well now the feed is an array we simply have to loop through it and we’ll have are RSS feed in an unordered list (or whatever format you want) on our page!

echo '
<ul id="facebook">';
 
foreach ($feeds as $feed)
{
	$date = strtotime($feed['pubDate']);
 
	echo '
	<li>';
	echo '
 
<strong><a href="http://www.facebook.com/'. $feed['link'] .'">FacebookPage</a></strong> '. $feed['description'] . ' '. date('jS F Y G:H' ,$date) .'
 
';
	echo '</li>
';
}
 
echo '</ul>
';

Don’t forget to style it up

I’ve written a few lines of CSS to make the feed look more ‘Facebook like’ if you so desire.

ul#facebook {
	padding: 10px 0 10px 0;
	margin: 0;
	list-style: none;
	font-size: 12px;
	font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
}
 
ul#facebook img { margin-right: 5px; }
 
ul#facebook li {
	padding: 10px 0 10px 0;
	margin: 0;
	overflow: hidden;
	border-bottom: solid 1px #E9E9E9;
}
 
ul#facebook li p {
	padding: 3px 0 3px 0;
	margin: 0;
	line-height: 18px;
}
 
ul#facebook a {
 
}
 
ul#facebook li a { color: #3B5998 !important; text-decoration: none; }
 
ul#facebook li a:hover { text-decoration: underline; }

Anyway, I hope you find this useful, ’cause I know I’ll be coming back to look at it soon enough. Leave any questions in the comments!

Author: Benjamin Reid

My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.

RSS Icon

Subscribe to the RSS feed some design goodness, get in on the upcoming software give-aways!

Digg Icon Share How to easily display a Facebook page’s feed on your website to Twitter Share How to easily display a Facebook page’s feed on your website to Delicious Share How to easily display a Facebook page’s feed on your website to Facebook

Related post(s)

Comments left by other wizards

66 Responses | Make a comment

Leave your words of wisdom

Leave a Reply

This is a Gravatar

Your name