Custom Facebook Feed based on Graph API

Photo of author
Written By Ryan Rana





First of all, this is a copy of my Stackoverflow answer here;

I really dislike Facebook. They constantly change the way things are done and make it difficult for developers to work out what the changes are and what the new methods should be. Designers don’t really appreciate this, they just stick in custom Facebook feeds on their scamps, the clients love them and then we have to work out how to do it!

It used to be easy to access an RSS feed from a public Facebook page. In truth it still is, the reason it can be hard for some is that all the blog posts and resources are out of date on it (‘how long with this post be relevant?’ I wonder to myself).

You don’t need to spend all day going round the official Facebook API documentation for something simple like a public Facebook feed. The confusion about oAuth tokens etc all arises because you’d assume that with all the keys and secret ids you generate when you register a Facebook App that you’d have to get permission or authentication from the Facebook page you wanted to pull the feed from – you don’t. All you need is a valid app and you can get the feed for any public page.

Set your app up in Facebook and it will give you an app id and an API key. Get the profile id for the public page feed you want, and that’s all you need. You can then use the following code to retrieve the auth token and then then use that to return the feed data as a JSON object.

function fetchUrl($url){

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 20);

 $feedData = curl_exec($ch);

 return $feedData;


$profile_id = "the_profile_id_of_the_page_you_want";

//App Info, needed for Auth
$app_id = "your_app_id_in_here";
$app_secret = "your_app_secret_in_here";

//Retrieve auth token
$authToken = fetchUrl("{$app_id}&client_secret={$app_secret}");

$json_object = fetchUrl("{$profile_id}/feed?{$authToken}");

You would then decode and parse the JSON object in PHP using the following code;

$feedarray = json_decode($json_object);

foreach ( $feedarray->data as $feed_data )
    echo "<h2>{$feed_data->name}</h2><br />";
    echo "{$feed_data->message}<br /><br />";

Leave a Comment