Adding a Facebook Status Feed to Your Website With PHP

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, now require an access token otherwise the json will display an error message.

So here goes...

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:

//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("".$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 = "$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);

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="'. $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.

Leave a Comment

All comments are appreciated. Comments are moderated before appearing on the site, so please don't bother trying leaving spam! Only genuine comments will be shown.

Your Name

Email Address (optional)

What year is it? (Spam Prevention)


Your Comments

Comment by Lauren on 18th August 2012 @ 4:08 pm (GMT)

How would I add a time stamp to this?

Comment by Mike Tycowski on 6th August 2012 @ 5:08 pm (GMT)

Sadly the above does not work, just yields a blank page. I feel that Facebook has changed the inner workings behind the scene. Probably did work at one point in time, it should be noted above that it no longer works.

Comment by Nicole on 5th July 2012 @ 11:07 pm (GMT)

Thank you! I appreciate the help.

Comment by Dean on 5th July 2012 @ 6:07 pm (GMT)

I will look into this more when I get time. The script worked when I did it last year, however Facebook keep changing their API without notice of any sort so I wouldn't be surprised if they have done something like that. I wrote this script because at the time, all of the other scripts on the net were failing due to an unexpected change in their API. Keep checking back for updates and good luck

Comment by Nicole on 5th July 2012 @ 6:07 pm (GMT)

Still nothing :(

Comment by Dean on 5th July 2012 @ 4:07 pm (GMT)

Try adding this:
$access_token = str_replace("|"," %7C",$access_token);

before this line:
$jsonurl = "$access_token";

Comment by Nicole on 5th July 2012 @ 4:07 pm (GMT)

Adding this: $jsonurl = url_encode("$access_token"); makes nothing happen.

Comment by Dean on 5th July 2012 @ 4:07 pm (GMT)

Oh! And turn full error reporting on so at least you can see what the problem is. It will make it much easier to trouble shoot.

Comment by Dean on 5th July 2012 @ 4:07 pm (GMT)

How about escaping the url?

$jsonurl = url_encode("$access_token");

Comment by Nicole on 5th July 2012 @ 4:07 pm (GMT)

It shows all of my statuses just fine if I put the URL in the browser. The problem is with the code. PHP isn't reading the |. I tried escaping it like this: "first_half_of_token\|rest_of_token" but that doesn't seem to work either. I really need to get this working. I followed your code exactly.

Comment by Dean on 4th July 2012 @ 8:07 pm (GMT)

What happens if you open the url in a browser with your token in it?

Comment by Nicole on 4th July 2012 @ 8:07 pm (GMT)

My auth token has a | (pipe or vertical bar) symbol in it. I'm pretty sure this is messing up everything because all I get are the echoed ul bullets. Any suggestions?

Comment by Dean on 29th May 2012 @ 12:05 pm (GMT)

@Andy - Try pasting the url in the address bar (replace the 2 variables with your actual IDs)$client_id&client_secret=$client_secret

That should return the access token, or an error message

Comment by Andy on 29th May 2012 @ 11:05 am (GMT)

Hi. Using the app access token doesn't seem to be working for me. When i run it directly in the browser it ells me I need a user access token. Any suggestions?

Comment by Dayanara on 18th August 2011 @ 7:08 am (GMT)

It's really great that people are sharing this ifnromation. business card printing123-reg Domain names

What's All This About?

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.

Recent Articles