PHP: Show your facebook status on your blog/website. (Works with new facebook)
Hello
After searching around on the internet for some time, I discovered that all of the previous scripts that had been made to do this job had been written for the old facebook which has long since been depriciated. I then started to consider how I would go about doing this with the NEW facebook since they removed the mini-feed which was vital to the old method.
After some fishing around in google, I resorted to copying links from images in old-method tutorials and stumbled upon this little gem: [url]http://www.facebook.com/minifeed.php[/url]. After trying out the link, and subsequently kicking myself I thought I was onto something. I was jumping for joy as I clicked "[b]Status Stories[/b]" and visited the "[b]Subscribe to these stories[/b]" link, which returned an RSS feed.
I then fired up my php editor, and typed something similar to the following
[php]<?php
$xml=simplexml_load_file(http://www.facebook.com/feeds/status.php?id=820080788&viewer=820080788&key=********&format=rss20);
print_r($xml);
?>[/php]
This returned any amount of horrible errors, and maybe you should try it for yourself to see what I mean. After a few days of poking around, I eventually tried to fetch the same page using my Lynx text-based web browser. What I found was that instead of return XML as it did for me (using IE, Firefox or Safari), a php page was shown with the message:
"You are using an incompatible web browser.
Sorry, we’re not cool enough to support your browser. Please keep it
real with one of the following browsers:
* Mozilla Firefox
* Opera
* Safari
* Microsoft Internet Explorer
* Flock"
Now I was onto something
. If Facebook requires the browser to be Firefox, Opera, Safari, IE or Flock (WTF is flock) then I must have to spoof my way in by pretending to use one of these browsers. Now I had to figure out how I was going to do this. After much trawling of [url=http://www.php.net]php.net[/url] and [url=http://www.google.com]google[/url] I found that there was no way to do it using the method I was previously using. After a few minutes of trying different things, I decided that I was going to fetch the xml file using curl and use the function [i]simplexml_load_file[/i] to load the string instead.
So off I went. Being unfamiliar with Curl, I set about trawling [url=http://www.php.net]php.net[/url] and came up with the following:
All the instructions + explanations are in the php comments (Lines beginning with //)
[php]<?php
// initialize a new curl resource
$ch = curl_init();
// set the url to fetch
curl_setopt($ch, CURLOPT_URL, ‘http://www.facebook.com/feeds/status.php?id=[FACEBOOK USER ID]&viewer=[FACEBOOK USER ID]&key=[ACCESS KEY (Get this by visiting this page in your browser from http://www.facebook.com/http://www.facebook.com/minifeed.php#/minifeed.php?filter=11)]&format=rss20′);
// don’t give me the headers just the content
curl_setopt($ch, CURLOPT_HEADER, 0);
// return the value instead of printing the response to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// use a user agent to mimic a browser
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0′);
// execute the curl command
$xml = curl_exec($ch);
//close the connection
curl_close($ch);
//load the result into a simplexml resource
$sxml = simplexml_load_string($xml);
//Get the title (I figured this out by using print_r($sxml)
$title = $sxml->channel->item->title;
//Same for the time
$tmk = $sxml->channel->item->pubDate;
//strtotime returns a integer timestamp which is more useful than our string (yesterday at xxx or whatever it was)
//I also cast to string using (string)$tmk as php was complaining about $tmk being a resource
$timestamp = strtotime((string)$tmk);
//Set the time back to my desired human-readable format
$time = date(‘d/m/y’, $timestamp);
//Italicise my name. You will have to change 11 to the correct length for your name. I get 11 from ‘<em>Stephen’
//To not italicise your name completely remove this line! \/
$title = substr_replace(‘<em>’.$title, ‘</em>’, 11, 0);
//Output the result
echo ‘<p>’.$time.’ – ‘.$title.’</p>’;
?>[/php]
And there we have it. If you would like to know more about this script, or I have forgotten something please leave me a comment. I hope this is of some use to you
Thanks
Stephen
22 Comments
Other Links to this Post
RSS feed for comments on this post. TrackBack URI
By monckfish, July 11, 2009 @ 10:25 pm
Nice one, I’ve been looking for something like this for ages. I hope you don’t mind if I use a version of the code
By xenocerenz, September 17, 2009 @ 8:28 am
hi,
is the code still work with current facebook?
http://www.facebook.com/http://www.facebook.com/minifeed.php#/minifeed.php?filter=11 had give me another link which is something like
http://www.facebook.com/facebook_name?v=feed
should it be something like this?thanks for the reply
By Stephen Groom, September 21, 2009 @ 8:09 am
Hey. That is the wrong link. The code should still work perfectly but ONLY if you can find the .xml link eg. http://www.facebook.com/feeds/status.php?id=820080788&viewer=820080788&key=********&format=rss20 but this is very difficult if not impossible on the new Facebook
Thanks
By pkake2000, January 22, 2010 @ 1:02 pm
Thanks, I’ll come back again.
By Jimmie Holst, March 5, 2010 @ 5:35 pm
Thank you for this nice article.
By Edris Asbell, March 6, 2010 @ 12:52 am
I need some advice for my blog….I like your layout. Can you help me?
By Dipanjan, March 7, 2010 @ 4:33 pm
Sir,
Is it possible to get friend’s image by user id after logging in using php-curl?
Regards
Dip
By Stephen Groom, March 8, 2010 @ 11:54 am
Hello,
This all depends on how yours and your friend’s facebook privacy settings are configured. I would say that it should be possible, but cannot think right now how to approach it.
Before experimenting with CURL, I would start with facebook’s API called Facebook Connect
http://wiki.developers.facebook.com/index.php/Facebook_Connect
Let me know how you get on
Thanks
Stephen
By Dip, March 10, 2010 @ 8:58 pm
$fn=uniqid().”my_cookies.txt”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php’);
curl_setopt($ch, CURLOPT_POSTFIELDS,’email=’.urlencode($_POST['email_box']).’&pass=’.urlencode($_POST['password_box']).’&login=Login’);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $fn);
curl_setopt($ch, CURLOPT_COOKIEFILE, $fn);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3″);
curl_exec($ch);
By Dip, March 10, 2010 @ 8:59 pm
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, ‘http://m.facebook.com/profile.php?id=’.$frnd_id);
$page = curl_exec($ch);
if(strpos($page,’http://profile.’)==false)
echo ”;
else
{
$profile_pic=substr($page,strpos($page,’http://profile.’),(strpos($page,’class=”p”‘)-strpos($page,’http://profile.’))-2);
$profile_pic=substr($profile_pic,strpos($profile_pic,’http://profile.’),strpos($profile_pic,’.jpg’)+4);
echo ”;
}
By Stephen Groom, March 10, 2010 @ 9:35 pm
Does that mean you resolved it?
Why are you using m.facebook.com, isn’t that for the mobile browser?
By Dip, March 11, 2010 @ 2:02 pm
That doesn’t matter wheather it’s for mobile browser or not. I was just trying to fetch each of my friend’s profile and from the output html I’m trying to get their respective pictures one by one.
But there is still some problems with it. Please browse the url and help me solve the problem.
http://accommercejpg.org/invite/
By Stephen Groom, March 11, 2010 @ 8:22 pm
The page is a contact-import. I don’t see the script present anywhere.
If you upload the script with error_reporting set to ON I will have no trouble having a pick through it for you
By Jona Seek, May 21, 2010 @ 7:36 am
Hello,Fantastic blogging dude! i am just Tired of using RSS feeds and do you use twitter?so i can follow you there:D.
PS:Have you thought to be putting video to this blog posts to keep the readers more entertained?I think it works., Jona Seek
By Jesusa Surowka, June 7, 2010 @ 10:20 am
Of course, what a wonderful internet site and informative posts, I’ll add backlink – bookmark this web page? Regards, Reader.
By Emil Mohamed, June 9, 2010 @ 8:56 am
Here is feed link that actually works: http://graph.facebook.com/USER ID]/feed
By Stephen Groom, June 10, 2010 @ 7:33 pm
Thankyou very much for this info!
By Kipu Mo, June 12, 2010 @ 2:13 pm
Very interesting, thanks for the information. I need to just keep building.
By Eura Pane, June 30, 2010 @ 1:45 pm
Some of the pictures were not displaying properly but, the site still looks great. I’ve been coming to this site for a few days now and i’m really impressed with the content. What is the feed address?
By Trying to Blog, July 5, 2010 @ 1:52 am
I’m greatly appreciative for the commitment to thisztopic that has been expertly presented to us in this post. I found your site on the Bing search engine and am very likely to subscribe to your RSS Feed if I see more content like this as I search through your old posts.
By Jordan, July 30, 2010 @ 9:49 pm
Here’s one that works w/ new FB policies
entry as $e)
{
if($limit>0){
echo “”;
echo “”. $e->content .”";
echo “Posted: “. date(“g:ia n/j/y”, strtotime($e->published)).”";
echo “”;
$limit–;
}
//print_r($e);
}
?>
By Beau, August 20, 2010 @ 4:50 pm
Jordan, can you please explain the new FB code you just posted in a little more detail.