Get Facebook like count using PHP

August 15, 2012 By 7 Comments

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.

Get Facebook like count

Facebook Fanbox

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:

Facebook Fanpage ID

Facebook Fanpage ID

After you get Facebook like count, you can display it as simple text using the following PHP code:

echo $fbcount.' like LinuXplained.com';

This will output 25 like LinuXplained.com.

You can even get counts of your Twitter followers and Feedburner subscribers and add them all together and display a combined followers count like this:

Combined Followers Subscribers Count

Combined Followers Subscribers Count

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.

Free E-Book: Build Your Own WordPress Site Guide
Last Updated: March 25, 2013

  • Don't miss out!

 

You may also like:

About Anand Subramanian

Anand is a self-learned computer enthusiast, a part-time blogger, and a Food Scientist by career. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, XBMC, and related HOW-TOs (read more).

Save on AKG K142HD Studio High Definition Semi-Open Headphones (Mocca/Sand)
Get It See All Deals