Can you get a public Facebook page’s feed using Graph API without asking a user to allow?

If you’re anything like me your clients won’t want a standard Facebook likebox plugin, they’ll want it all styled and customised their own way.

You don’t need to spend all day going round the official documentation wondering if any of it applies to you for something simple like this, it’s quite easy. The confusion arises because you’d assume that with all these keys and secret ids 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.

<?php

function fetchUrl($url){

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
 // You may need to add the line below
 // curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

 $feedData = curl_exec($ch);
 curl_close($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("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret={$app_secret}");

$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");

Thanks to an edit someone suggested I reckon this code came from here (looks familiar anyway 🙂 ) and there’s some more info in the comments there that might help.

You can then parse the object, here’s some code to do it in PHP based on this thread;

Handling data in a PHP JSON Object

$feedarray = json_decode($json_object);

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

To find out what’s available to you in the json object you can output the url in a browser and copy/paste it into this useful json visualisation tool;

http://chris.photobooks.com/json/default.htm

Leave a Comment