How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

October 8, 2012


When you add facebook like button to your site, probably, you also want to save the number of likes of your pages to your database.These data are very useful and very important because they tell you which of your articles are better than others. Moreover, these pages are better than others because the user has decided to tell his friends about it.

So you can use these likes to make a top stories section on your site as I make on the top of DailyBest.

You can read these data because facebook likes and facebook shares are public data in Open Graph, you don’t need any authentication process to read likes, they are public and you can easily read facebook likes count with php or with jquery. Here is the code to read facebook likes and shares count with php.

function readFacebookLikes($url) {
	$query = "select total_count from link_stat WHERE url ='" . $url ."'";
	$s = file_get_contents("https://api.facebook.com/method/fql.query?query=".
             urlencode($query)."&format=json");
	preg_match("#(\"total_count\"):([0-9]*)#",$s,$ar);
	if(isset($ar[2])) return $ar[2]; else return null;
}
echo readFacebookLikes("https://www.facebook.com/rockit.tuttarobaitaliana");
echo readFacebookLikes("http://www.dailybest.it");

This function will be added to Minibot class.

As you can see in the code you can justĀ make a query using the fql language (it’s very similar to normal sql language) and retrieve data from the theĀ facebook link_stat table. You can retrieve different data, I’ve used “total_count”:

share_count The number of times users have shared the page on Facebook.
like_count The number of times Facebook users have “Liked” the page, or liked any comments or re-shares of this page.
comment_count The number of comments users have made on the shared story.
total_count The total number of times the URL has been shared, liked, or commented on.
click_count The number of times Facebook users have clicked a link to the page from a share or like.

 

This tecnique is used in my Top Social Stories Plugin for WordPress to build charts and monitor your top stories.

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “How to read facebook likes count from PHP”

5 thoughts

  1. Giulio Pons says:

    that works too, but it has only the shares. using a query with total count gives you a data that includes also likes and comments.

  2. darvin says:

    i tried the codes but it has an error:
    Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP? in C:\xampp\htdocs\searchmydroid\home.php on line 41

    Warning: file_get_contents(https://api.facebook.com/method/fql.query?query=select+total_count+from+link_stat+WHERE+url+%3D%27https%3A%2F%2Fwww.facebook.com%2Frockit.tuttarobaitaliana%27&format=json) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\searchmydroid\home.php on line 41

  3. David says:

    This way you will have only the count :)

  4. pope says:

    another way is to use the facebook graph api: http://newexception.com/facebook-like-counter

Comments are closed

Recommended

Make a cron job with IFTTT

Cron is a software utility, a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain…

November 12, 2013

How to use Instagr.am photos on your site

UPDATE: 2013-12-04 I’ve made a method in the Mini Bots PHP Class that lets you retrieve images from instagram without…

August 18, 2011

PHP to post on a Facebook page

Hi, I’ve modified the Mini Bot Class, I’ve fixed the Facebook status update and I’ve implemented the function to post…

July 28, 2010

PHP curl bot to update Facebook status

I’ve found this great mini bot from Alste blog, and I’ve decided to add it to the mini bot class.…

March 1, 2010

Scraping content with PHP as if it was jQuery

Building a spider or a bot needs some knowledge of regular expressions, you must know and use preg_match or preg_match_all…

December 8, 2013

Get instagram data without official api in PHP

Instagram has an official API to interact with its database of images and users. If you have enough time to…

December 3, 2013