read-facebook-like-count

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

  1. How many times a web link has been shared on Twitter
  2. Posting to Facebook from website with Facebook Connect
  3. Facebook Connect Tutorial, new version
  4. PHP curl bot to update Facebook status
  5. Facebook Fan Gate Tutorial

5 comments

  1. darvin

    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

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>