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…

Ottobre 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

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

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

5 thoughts

  1. Giulio Pons ha detto:

    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 ha detto:

    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 ha detto:

    This way you will have only the count :)

  4. pope ha detto:

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

Comments are closed

Recommended

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

Ottobre 19, 2012

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…

Novembre 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…

Agosto 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…

Luglio 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.…

Marzo 1, 2010

Find values recursively inside complex json objects in PHP

A PHP function to to quickly search complex, nested php structures for specific values.

Dicembre 18, 2022