9th June 2011
UPDATE: This code no longer works as it appears that Facebook has made some changes at their end. If I get the time, I will find out what has changed and post an updated working version.
If you would like to add your latest Facebook wall post feed to your website, you will probably have noticed that there isn't a widget available like there is with Twitter. This short tutorial will show you how you can add a feed using Facebook's Open Graph Protocol. We will also be caching the feed to increase page load times, avoiding the call to facebook for every page load.
Up until a few days ago, this was a lot more straight forward, however...you now require an access token otherwise the json will display an error message.
Firstly create a folder named 'cache' and create an empty file inside it called cachefile.json. This is where we will be storing the feed locally. Make sure the folder and file are writable.
//set your cache file path as a variable
$cachefile = 'cache/cachefile.json';
//set the length of time between renewing the cache file in seconds (2 hours in this case)
$cachetime = 7200;
//Read from the cache file if it's less than 2 hours old
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
$json = file_get_contents($cachefile,0,null,null);
$json_output = json_decode($json, true);
}
This next part will retrieve the Facebook feed and write it to the cache file if it is older than 2 hours. We also need to get an access token, which unfortunately requires you to create an app. Once you create your app, you will see your app_id and client_secret code for use in this next part:
else{
//Here we will obtain the access token automatically
//set your app id and client_secret codes from your facebook app
$client_id = 'YOUR_APP_ID';
$client_secret = 'YOUR_CLIENT SECRET';
//send a request for an access token and get the results
//it should return something like "access_token=HjIHb9g8u4bfnietc..."
$access_token = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=".$client_id."&client_secret=".$client_secret);
//Now apply the access token and request the feed. I have limited the feed to the most recent 7
//Don't forget to set your facebook name or id in the url
$jsonurl = "https://graph.facebook.com/YOUR_FACEBOOK_ID/feed?limit=7&$access_token";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
//Now write the json results to the cache file
$fp = fopen($cachefile, 'w');
fwrite($fp, $json);
fclose($fp);
}
Finally, we read the json results from the cache file and lay it out in a readable format. You may want to look at the cache file to see the array structure and lay it out however you like:
echo "<ul>";
for($i=0; $i<7; $i++)
{
echo "<li>";
if(isset($json_output['data'][$i]['from']['name'])){echo '<span class="fbName"><a href="http://www.facebook.com/profile.php?id='. $json_output['data'][$i]['from']['id'].'">'.$json_output['data'][$i]['from']['name'].'</a></span>';}
if(isset($json_output['data'][$i]['message'])){echo "<br />". $json_output['data'][$i]['message'];}
if(isset($json_output['data'][$i]['link'])){echo "<br /><a href=\"". $json_output['data'][$i]['link'] ."\">".$json_output['data'][$i]['name']."</a>";}
if(isset($json_output['data'][$i]['description'])){echo "<br />".$json_output['data'][$i]['description'];}
if(isset($json_output['data'][$i]['properties'][0]['text'])){echo "<br />".$json_output['data'][$i]['properties'][0]['text'];}
if(isset($json_output['data'][$i]['properties'][1]['application'])=='Events' && isset($json_output['data'][$i]['properties'][1]['text'])){echo "<br />".$json_output['data'][$i]['properties'][1]['text'];}
echo "</li>";
}
echo "</ul>";
And that is it! Facebook isn't very friendly when it comes to custom integration and their documentation is awful. They also seem to give no notice when making changes, so don't be surprised if it stops working one day and you have to go back and alter it to suit whatever changes they have made. Hopefully this will help you along the way for now.
Deanblog is a collection of articles written by Dean Morgan from Deanzod Limited. The aim of the site is to provide helpful information for everyone from web designers & developers right through to website owners. I will try and keep a good balance of information such as php/mySQL tutorials, html/css tutorials along with marketing ideas and advice for website owners.
Your Comments
Comment by Lauren on 18th August 2012 @ 4:08 pm (GMT)
Comment by Mike Tycowski on 6th August 2012 @ 5:08 pm (GMT)
Comment by Nicole on 5th July 2012 @ 11:07 pm (GMT)
Comment by Dean on 5th July 2012 @ 6:07 pm (GMT)
Comment by Nicole on 5th July 2012 @ 6:07 pm (GMT)
Comment by Dean on 5th July 2012 @ 4:07 pm (GMT)
Comment by Nicole on 5th July 2012 @ 4:07 pm (GMT)
Comment by Dean on 5th July 2012 @ 4:07 pm (GMT)
Comment by Dean on 5th July 2012 @ 4:07 pm (GMT)
Comment by Nicole on 5th July 2012 @ 4:07 pm (GMT)
Comment by Dean on 4th July 2012 @ 8:07 pm (GMT)
Comment by Nicole on 4th July 2012 @ 8:07 pm (GMT)
Comment by Dean on 29th May 2012 @ 12:05 pm (GMT)
Comment by Andy on 29th May 2012 @ 11:05 am (GMT)
Comment by Dayanara on 18th August 2011 @ 7:08 am (GMT)