The Facebook fanbox is pretty neat. It displays the Facebook like count as well as the pictures of the fans. One drawback to it is that it occupies a lot of space on your blog. In this post, I am going to explain how to get Facebook like count for your fan page with a simple PHP script. Check my other post for getting Twitter followers count using PHP. Another major drawback is that a fancy Facebook fanbox, which displays stream, faces, etc. will delay the page loading significantly. For this reason you may find that many of the blogs just provide a link to their Facebook fan page. I personally like to provide a like button on the blog, which makes it convenient for the visitors to like the page with just a couple of clicks.
PHP code to get Facebook Like Count
The PHP script to get Facebook like count is pretty simple and straightforward:
$fpageID = '133529600097002'; $finfo = json_decode(file_get_contents('http://graph.facebook.com/' . $fpageID)); $fbcount = $finfo->likes;
$fpageID is the variable to hold your fanpage ID. You can get it from your Facebook fanpage:
After you get Facebook like count, you can display it as simple text using the following PHP code:
echo $fbcount.' like htpcbeginner.com';
This will output
25 like htpcbeginner.com.
Facebook Like Count History
Although you may be able to get the Facebook like count history and more by other means, I like to record the data in a format I like. I currently get Facebook like count, Twitter followers count, FeedBurner subscribers count, and Alexa site rank and record them in a MySQL database daily to see the growth of my blog. If you make any changes / improvements to your blog then you have the data to see what impact it had on your followers count. In a separate post, I will explain how to accomplish this.
Meanwhile, get Facebook like count using the above script, get creative, and find innovative ways to use the count. Please do not forget to share your idea with us in the comments sections.