Get Facebook like count using PHP

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 htpcbeginner.com';

This will output 25 like htpcbeginner.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.

Be the 1 in 200,000. Help us sustain what we do.
113 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.

Try Deployarr