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:
- Go to your Facebook Profile
- Click on the “Mini-Feed” bar so you can see all of your recent activity
- Select the “See All” link on the top right of the “Mini-Feed” bar
- Under “View” on the right, you will see a list of “Stories”, click on “Status Stories”
- 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:
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.
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?
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()};
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.
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
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
@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.
@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.
I’m havning problem with the text-encoding. How can I change the output encoding in PHP?
Regards
Erik
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!
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?
@Ian Tearle - Fine with me. Just give credit where it is due! Good luck.
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!
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!
@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.
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
Any words of wisdom would be appreciated, I spent all night trying to figure it out, and I am now braindead. Thanks in advance!