Facebook RSS Feed

By: Christian Blanquera

So Facebook apparently has an undocumented feature which allows you go get RSS feeds from public walls.
https://www.facebook.com/feeds/page.php?id=[YOUR FACEBOOK ID&format=rss20
This is great however Facebook limits their feeds to browser access only. Creating a feed reader in PHP you will get an error. For example, the code below:

Simple Get Content
echo file_get_content('https://www.facebook.com/feeds/page.php?id=190029037738338&format=rss20');

.. would result in ..

You are using an incompatible web browser.

Sorry, we're not cool enough to support your browser. Please keep it real with one of the following browsers:

  • Mozilla Firefox
  • Google Chrome
  • Safari
  • Microsoft Internet Explorer

The way to get around this is by setting the user agent to one of the specified browsers in the error. We can set user agents with the use of curl. So our code would look something similar to the following.

cURL vs Facebook ... Fight!
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.facebook.com/feeds/page.php?id=190029037738338&format=rss20');
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$xml = curl_exec($curl);

curl_close($curl);

From here you can use DOMDocument or SimpleXML to parse that return response in this format.

$response = simplexml_load_string($xml);

With Eden you can do all the above in one chain!

With Eden
$response = eden('curl')
	->setUrl('https://www.facebook.com/feeds/page.php?id=190029037738338&format=rss20')
	->setUserAgent('Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13')
	->setConnectTimeout(10)
	->setFollowLocation(true)
	->setTimeout(60)
	->verifyPeer(false)
	->getSimpleXmlResponse();

From here you can parse and output the XML like so.

foreach($response->channel->item as $post) {
	$time = strtotime($post->pubDate);
	$published = date('F d, Y g:i a', $time);
	
	echo 'Title: '.(string)$post->title.'<br />';
	echo 'Description: '.(string)$post->description.'<br />';
}