Everyone now-a-days has their latest tweets on their website. How many sites do you see that have their latest facebook status update (besides this one)? Not very many. Well, there’s a reason for that. Facebook’s documentation for developers is just plain awful. Yes, they make it accessible in a few different programming languages, but if you don’t have a very firm understanding of the API and some of the things they require like OAuth 2.0, you’ll spend a lot of time scratching your head. That’s what happened to me.
The way my personal site was designed, I wanted to have my latest tweet, latest blog post, and latest Facebook status update displayed in the upper right-hand corner. That approach simply flowed with what I wanted to accomplish. I soon came to realize that this was something much harder than I thought–Mainly due to my limited understanding of the OAuth 2.0 protocol and Facebook’s severely lacking documentation.
It took me long enough to get it working and I don’t want to waste your time with fluff so, without further delay, here’s what you do to get your latest Facebook status update on your website.
- Set up a new Facebook application.
- Keep your Application ID and Application Secret handy.
- Download the Facebook PHP SDK and put it in a folder on your server that you’ll remember for later.
- Grant the application access to your Facebook account (more info here).
- Get an Access Token (the hard part–if you’re having trouble with this part, see my other post or use the Access Token Generator I created).
- Use the following code to pull in your Facebook status (Don’t forget to set the variables at the top with your Application ID, Application Secret, and the User ID of the user whose status you want. Also, you can pull in other information also, options are listed here)
<?php
//facebook.php needs to link to the PHP SDK you downloaded from GitHub (step 3)
require ‘facebook.php’;
//set necessary variables
$appID = ”;
$appSecret = ”;
$accessToken = ”;
$uid = ”;//begin requesting status
$facebook = new Facebook($appID,$appSecret);
$f = fopen (“https://api.facebook.com/method/status.get?uid=’$uid’&limit=1&access_token=’$accessToken’”, “r”);while ($line= htmlentities(fgets($f))) {
if ($line===FALSE) print (“FALSE\n”);
else
{
$content = $content.” “.$line;
}
}
fclose ($f);$message = explode(“<message>”, $content); // search for the tag
$message_cnt = count($message);
$msg_index = 0;$time = explode(“<time>”, $content); // search for the tag
for($i=1; $i<$message_cnt; $i++)
{
$tmp = explode(“</message>”, $message[$i]);
$msg[$msg_index] = $tmp[0]; // status message$tmp2 = explode(“</time>”, $time[$i]);
$t[$msg_index++] = $tmp2[0]; // time of posting
}for($i=0; $i<$msg_index; $i++)
{
echo(““.preg_replace(‘!\015\012|\015|\012!’,’
‘,$msg[$i]).”
\n”);
?>
Once you’ve got the above script saved on your server, you’ll need to include it on the page you wish to have your status displayed. Remember, this snippet of code below needs to be placed where you want the status to be displayed. You can’t just stick it at the bottom of your code.
<?php
//this is the file that contains the above code, the file in ” should be changed to whatever you named the above script.
include(‘getStatus.php’);
?>
That’s it! If you’ve given your application sufficient access to your Facebook account, you should now be seeing your latest status updates on your website. If you have any questions, shoot me an email or leave a comment. Enjoy!
Pingback: Will Boling » Blog Archive » Get Your Latest Tweets on Your Website
Most of the times blogs are the same but i honestly enjoyed what i read. Bravo !