PHP: Show your facebook status on your blog/website. (Works with new facebook)

November 14, 2008

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

12 Responses to “PHP: Show your facebook status on your blog/website. (Works with new facebook)”

  1. monckfish says:

    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

  2. xenocerenz says:

    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

  3. 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

  4. pkake2000 says:

    Thanks, I’ll come back again.

  5. Jimmie Holst says:

    Thank you for this nice article.

  6. Edris Asbell says:

    I need some advice for my blog….I like your layout. Can you help me?

  7. Dipanjan says:

    Sir,
    Is it possible to get friend’s image by user id after logging in using php-curl?
    Regards
    Dip

  8. Dip says:

    $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);

  9. Dip says:

    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 ”;

    }

  10. Does that mean you resolved it?

    Why are you using m.facebook.com, isn’t that for the mobile browser?

  11. Dip says:

    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/

Leave a Reply