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("".
	if(isset($ar[2])) return $ar[2]; else return null;
echo readFacebookLikes("");
echo readFacebookLikes("");

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.

Anything to say?

Comments with Facebook or below with WordPress

WordPress comments:

  1. 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. 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( [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\searchmydroid\home.php on line 41

Sorry, WordPress comments are closed on old posts.