Stats2Site: Add Facebook Status To Your Website

Computers, Facebook, How To's, Internet Add comments

I get a lot of people coming to my website doing Google searches on this topic, so here is the answer, and it’s called Stats2Site. I myself like to use Twitter, but I realize that not everyone likes to do that (thank goodness I care about you!).

There already exist ways to get your Facebook status on your Wordpress or Drupal blog, but what if you are not using either? Lucky for you I created a PHP function that will allow you to post your Facebook status on any site regardless of the underlying CM system.

Stats2Site is a small function written in PHP that will echo your Facebook status (or your friends for that matter) anywhere in your HTML code. You can display as many of your status updates as you like, have the option to show or remove the time-stamp on a status update, and can even remove the “Name is” if you choose.

Get the Code

Download Stats2Site here. At the moment this solution only works with PHP5. I will be working to get it compatible with PHP4.

Configure Stats2Site

Stats2Site needs to be configured before you can use it. You will need to add your Facebook Status RSS feed and your Facebook Name, i.e. the name that shows up in your status updates. To get your Facebook Status RSS feed do the following:

  1. Go to your Facebook Profile
  2. Click on the “Mini-Feed” bar so you can see all of your recent activity
  3. Select the “See All” link on the top right of the “Mini-Feed” bar
  4. Under “View” on the right, you will see a list of “Stories”, click on “Status Stories”
  5. Underneath the list on the right, you will see “Subscribe to these Stories - My Status”, right-click on “My Status” and choose “Copy Link Location”

You will then paste this link into Stats2Site for $url. You will also need to add your name into $fbname. Those are the essentials. You can also modify the prefixes/suffixes to each status update, change the number of updates you show, and turn on/off the time-stamp.

Calling Stats2Site

Now you’ve got Stats2Site setup all you need to do now is place it on your website. To do this simply include the Stats2Site file in your webpage and then call the function put_Stats2Site() where ever you would like it to show up. For example:

<?php
include ‘Stats2Site.php’;

if(function_exists(‘put_Stats2Site’)){put_Stats2Site();}

?>

Conclusions

From my experience, this method works quite well with Facebook. As I currently write this, Facebook is having issues with their RSS feeds and have an extra “is” in the update. Hopefully this will clear up soon. In terms of latency, my updates will appear immediately after I change them in Facebook.

If you have any problems or questions, please post them to the comments. Good luck.



37 Responses to “Stats2Site: Add Facebook Status To Your Website”

  1. Hey Blake, great tool, good tutorial by way of reading your code, i see whats happening, bar the fact i have never used CURL before, im getting this error:

    Call to undefined function curl_init()

    Any ideas? do i need to add anything to my PHP setup?

  2. Sorry, cleared that last problem up, but now my updates only appear at the top of the page, cant seem to get them where i want them, im adding it into a sidebar in a template system:

    $Sidebar.=
    include(”includes/Stats2Site.php”);
    if(function_exists(’put_Stats2Site’)){put_Stats2Site()};

  3. Are you saying with the $Sidebar code, it still shows at the top?

    I would put the “include” statement at the top of my webpage code. Then in the sidebar just call the put_Stats2Site() function. You might also try removing the if part and just call the put_Stats2Site() function. If it crashes, you will get the php error and know why.

  4. Sorry, changed the code slightly to that of previously posted, very bad php! I included the include at the top of the page, then altered the rest to this:

    if(function_exists(‘put_Stats2Site’)){
    $Sidebar.= put_Stats2Site();
    }

    But still the results returned appears above everything on the page, including the doctype!! I have tried this on another website, different server etc setup and slightly newer version of PHP and although it shows where you would expect it to show, i do get an error, which i am trying to resolve with my host, the error just incase you are interested is:

    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /mnt/gs01/herd03/2181/domains/iantearle.com/html/Stats2Site.php:52 Stack trace: #0 /mnt/gs01/herd03/2181/domains/iantearle.com/html/Stats2Site.php(52): SimpleXMLElement->__construct(”) #1 /mnt/gs01/herd03/2181/domains/iantearle.com/html/index.php(256): put_Stats2Site() #2 {main} thrown in /mnt/gs01/herd03/2181/domains/iantearle.com/html/Stats2Site.php on line 52

  5. I also get the “Fatal error: Call to undefined function curl_init() in /var/www/Stats2Site.php on line 35″ message… Any idea how I fix it?

    Thanks

  6. @Ian Tearle, sorry for the late reply. Line 52 is creating an XML element which is only supported by PHP5. I have tested it on PHP5.2.5 and cannot be sure if prior version of PHP5 support the XML Element.

  7. @Dom The curl_init() function was introduced in PHP4.0.2 and is available in PHP5. Make sure your server is using at least PHP4.0.2. If you are using a web hosting service, most of them give you the option to use PHP4 or PHP5. Change it to PHP5 if you don’t have any applications that require PHP4.

    Let me know how it goes.

  8. I’m havning problem with the text-encoding. How can I change the output encoding in PHP?

    Regards
    Erik

  9. Thanks Blake - I’m running my own webserver (but new to this kinda thing) and added the Curl functionality to my PHP installation. Everything works perfectly now, so many thanks!

  10. Just wanted to say thank you Blake, this is an excellent tool to display Facebook status updates on your own site, I managed to resolve the issues i was having, PHP 5 and some editing on my CMS (Expanse now Dreamscape - dreamscapecms.com for the inquisitive)- to which between my self and the now owner are creating a plugin to, if that would be ok Blake?

  11. @Ian Tearle - Fine with me. Just give credit where it is due! Good luck.

  12. Is it possible to enable cache for the script? it seems to be the only thing thats slowing my page down at the moment.

    Thanks for a awsome script!

  13. Hey Blake, any news on being able to convert this script to PHP4?
    I will be working to get it compatible with PHP4.

    I’ve tried myself, but failed. I would prefer to use your script as it grabs the feed direct rather than relying on twitter.
    I’ll buy you a beer if you can do it!

  14. @Ian Tearle I would love to get this working with PHP4 but my company is really taking off and I have been dedicating all my resources to that.

  15. Hi Blake,

    This seems like the perfect tool for me. However, I’m very limited at using PHP. I’d love it if you could help me out.

    I’ve inserted this code in multiple places in my index.html page, with the call. All it returns is:

    Warning: main(‘Stats2Sitephp’) [function.main]: failed to open stream: No such file or directory in /home/.terkel/natechristman/copyreader.net/index.html on line 14

    Warning: main(‘Stats2Sitephp’) [function.main]: failed to open stream: No such file or directory in /home/.terkel/natechristman/copyreader.net/index.html on line 14

    Warning: main() [function.include]: Failed opening ‘‘Stats2Sitephp’’ for inclusion (include_path=’.:/usr/local/lib/php’) in /home/.terkel/natechristman/copyreader.net/index.html on line 14

    I’m super confused on where to put the function definition, so that the call will find it. I think that’s my only problem :D Any words of wisdom would be appreciated, I spent all night trying to figure it out, and I am now braindead. Thanks in advance!

  16. I have been having some trouble with this script. It does work, but prevents my page from loading most of the time. Other times it just takes forever to load…

  17. Hey mate, I’m real confused, i’ve been trying to hunt this down for a while now, and i don’t know whether it’s because i don’t know what im loking for or it’s no longer available. The “mini-feed” RSS feed that everyone keeps talking about, I can-not find at all on the new Facebook. If someone can direct me to where it is, following the instructions on this site has left me somewhat at a loss.

  18. @Martin - Please visit http://www.new.facebook.com/minifeed.php?filter=11 and look for the “My Status” feed at the bottom.

  19. Hi Blake, got the code sorted, now I’ve got the same problem as Nate from August 7th. any help would be appreciated.

  20. Hey Martin,

    Just want to check. You have got PHP5 installed haven’t you? The script will only work with PHP5 as simpleXML is not supported in any less than libraries.

    Hope this works out for you.

    Stats2Site plugin for DreamscapeCMS and Expanse available, visit http://blog.iantearle.com

  21. Hi there. Also got the curl_init problem. I am 100percent sure that PHP5+ is running on my server so I wonder what the reason might be that the function is not working…

  22. Hi Blake,

    I’m getting:
    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /home/pbaker/public_html/philsbury/new/includes/facebook.php:52 Stack trace: #0 /home/pbaker/public_html/philsbury/new/includes/facebook.php(52): SimpleXMLElement->__construct(”) #1 /home/pbaker/public_html/philsbury/new/index.php(54): put_Stats2Site() #2 {main} thrown in /home/pbaker/public_html/philsbury/new/includes/facebook.php on line 52

    I’ve read the previous posts and you say this is only in PHP5, I’m running 5.2 - any ideas?

    Cheers
    Phil

  23. Hi Phil et al., I am definitely running PHP5+ on my site but like Andreas I am getting the same problems still. I start my programming subjects at uni in March, I think I’m going to leave all this until I develop some skills beyond a very basic understanding of coding.

  24. Hi, your code works really great. I would like to be able to put 4 facebook user’s status on my blog (my wife’s, my 2 kids and I) How can I do that with your code ?

    If someone has an idea ????
    thanks

  25. Hi Blake, like Martin I’m running PHP5 but also experience the ‘uncaught exception’ problem. Any more suggestions?

    Regards,

    Andy

  26. Sapin,

    To add more than one Facebook user’s status all you’ll need to do is have function variables, like:

    function put_Stats2Site($url,$fbname,$numofupdates)

    Then remove these 3 variables from the top of the function.

    On your page instead of just

    put_Stats2Site();

    You’ll need to fill in the variables like

    put_Stats2Site($url_1,$fbname_1,$numofupdates_1);
    put_Stats2Site($url_2,$fbname_2,$numofupdates_2);
    put_Stats2Site($url_3,$fbname_3,$numofupdates_3);
    put_Stats2Site($url_4,$fbname_4,$numofupdates_4);

    Setting all the _1 variables to one person, _2 to another person, etc…

    ALSO, whilst I’m commenting, the code has been displaying my status update on my website fine up until today, but now it’s not showing. Any reasons why this may be?

    Please note that my last 3 updates have :) in them, could this be stopping it from working?

    My website is peternichol.com

    And my facebook status update feed is
    http://new.facebook.com/feeds/status.php?id=504065984&viewer=504065984&key=d8f9b9fac8&format=rss20

    Thanks.

    -Peter

  27. Martin,

    On your PHP file try giving it a .php5 extension.

    My webserver is both PHP 4 and PHP 5 enabled, but defaults to PHP 4. To display a PHP 5 page I just use “.php5″

    Give it a go!

  28. Hi guys, I’ve managed to fix the Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’…….error.

    find the line :

    curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.1) Gecko/20061223 Firefox/2.0.0.1″);

    and add this after it:
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    Working fine now!

    :)

  29. Hmmm everything works great! Except I can’t get rid of the name, no difference if I set $rmnameis to true or false. Any ideas?? Thanks!

  30. OK fixed it…

    $rmnameis = true;

    $message = str_replace($fbprefix,null,$xml->channel->item[$count]->title);

    replaced by

    $message = str_replace(’My name ‘,null,$xml->channel->item[$count]->title);

  31. @Peter Nichol

    are sure your code works ? I can not make it work. I don’t understand where to put the variable. Also there are references to $fbname at lines 61 and 119. What should I do with it ?

    Thanks

  32. Is there a chance to display ONLY days (OR hours OR minutes)? For example:

    “Updated about 2 days ago”
    instead of
    “Updated 2 days, 14 hours, 12 minutes ago”

    or

    “Updated about 14 hours ago”
    instead of
    “Updated 14 hours, 12 minutes ago”

  33. I had to upgrade to PHP5 in order for it to work - but it’s awesome.

    Thanks for the great bit of code

  34. Where is the Mini-Feed bar?

  35. I am trying to implement Stats2Site on a joomla site. I’ve placed the following code into a page which is a file called default.php:

    I’ve verified that the include is working, but I am getting the following error message:
    Fatal error: Call to undefined function put_stats2site() in /default.php on line 3

    I have not altered the Stats2Site php file, except to input the variables. Joomla is running PHP 5.2.9.

    Can you tell me what I am doing wrong?

    (By the way, the error message outputs the function name in all lowercase. I thought maybe somehow the cases were getting screwed up, causing the error. But I changed the function name to all lowercase, both in the Stats2Site.php file and in the call to the function within default.php, and get the same error)

  36. I am trying to implement Stats2Site on a joomla site. I’ve placed the following code into a page which is a file called default.php:

    I’ve verified that the include is working, but I am getting the following error message:
    Fatal error: Call to undefined function put_stats2site() in /default.php on line 3

    I have not altered the Stats2Site php file, except to input the variables. Joomla is running PHP 5.2.9.

    Can you tell me what I am doing wrong?

    (By the way, the error message outputs the function name in all lowercase. I thought maybe somehow the cases were getting screwed up, causing the error. But I changed the function name to all lowercase, both in the Stats2Site.php file and in the call to the function within default.php, and get the same error)

Leave a Reply

Copyright © 2010 - Blake Brannon
WP Theme by N.Design Studio
Entries RSS Comments RSS Log in