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
July 11th, 2009 - 22:25
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
September 17th, 2009 - 08:28
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
September 21st, 2009 - 08:09
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
January 22nd, 2010 - 13:02
Thanks, I’ll come back again.
March 5th, 2010 - 17:35
Thank you for this nice article.
March 6th, 2010 - 00:52
I need some advice for my blog….I like your layout. Can you help me?
March 7th, 2010 - 16:33
Sir,
Is it possible to get friend’s image by user id after logging in using php-curl?
Regards
Dip
March 8th, 2010 - 11:54
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
March 10th, 2010 - 20:58
$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);
March 10th, 2010 - 20:59
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 ”;
}
March 10th, 2010 - 21:35
Does that mean you resolved it?
Why are you using m.facebook.com, isn’t that for the mobile browser?
March 11th, 2010 - 14:02
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/
March 11th, 2010 - 20:22
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
May 21st, 2010 - 07:36
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
June 7th, 2010 - 10:20
Of course, what a wonderful internet site and informative posts, I’ll add backlink – bookmark this web page? Regards, Reader.
June 9th, 2010 - 08:56
Here is feed link that actually works: http://graph.facebook.com/USER ID]/feed
June 10th, 2010 - 19:33
Thankyou very much for this info!
June 12th, 2010 - 14:13
Very interesting, thanks for the information. I need to just keep building.
June 30th, 2010 - 13:45
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?
July 5th, 2010 - 01:52
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.
July 30th, 2010 - 21:49
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);
}
?>
August 20th, 2010 - 16:50
Jordan, can you please explain the new FB code you just posted in a little more detail.
May 21st, 2011 - 00:06
Hey! I realize this is kind of off-topic but I had to ask. Does managing a well-established website like yours require a massive amount work? I am completely new to running a blog but I do write in my diary on a daily basis. I’d like to start a blog so I will be able to share my personal experience and thoughts online. Please let me know if you have any recommendations or tips for new aspiring blog owners. Appreciate it!