How to Get the Number of Facebook Likes on a Joomla Website?

We were asked to do a very challenging task by one of our clients: He wanted to get the number of Facebook “likes” his website has on every page. Of course, this can be determined by simply placing a Facebook “Like” button on every page (using a simple content plugin), but our client wanted to have a module on his website containing the top 10 “liked” pages on Facebook, so this meant that we needed to get that information programmatically. That job was challenging because we’ve never done it. However, we were confident that we were able to do it.

So, to satisfy our client’s requirements we needed to:

  1. Get the number of Facebook likes he has on each and every page of his Joomla website.
  2. Store the above information in the Joomla database.
  3. Create a module that will list the “Top 10 Liked Pages on Facebook”.
  4. Display the above module on every page.

We will only examine the first step (as the other steps are trivial considering the complexity of the first step) and we will explain, in details, how it can be done.

First thing that you need to know (it took us some time to discover it) is that Facebook has an external interface that allows anyone to get the number of Facebook likes for any page, for example, here’s the one for Google:

http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27http://www.google.com%27

Copy the above link into your browser’s address bar and you’ll see something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
	<link_stat>
		<like_count>796902</like_count>
	</link_stat>
</fql_query_response>

As you can see from the above, the URL returns an XML response containing the number of likes that the page www.google.com received. At the time of writing this post, www.google.com had 796,902 likes (that number is very low considering the amount of traffic Google has, but it might be because Google doesn’t have a Facebook Like button on its homepage – we are confident that the number is accurate because we did many tests with many other websites and all the tests returned accurate results).

Now, let’s try to do the same with www.facebook.com. Copy the below URL into your address bar:

http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27http//www.facebook.com%27

…and you will get the below XML response:

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
	<link_stat>
		<like_count>2292310</like_count>
	</link_stat>
</fql_query_response>

We know from the above that www.facebook.com has 2,292,310 likes. Now, pay attention that this number doesn’t mean that the total likes for all the pages on Facebook is just over 2 million, it means that the homepage, and just the homepage, has this amount of likes.

By now you’ve probably guessed it, in order to the number of likes of a certain page, you only have to call the following URL from the code and parse its XML response:

http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27{page-url}%27

Where {page-url} can be any page on your website!

So, in order to gather the number of Facebook Likes for every page on our customer’s Joomla website, we created a system plugin, called “Count Facebook Likes”, and we made sure that the plugin ran on every page. That plugin did the following:

  • Invoked the above API with the current page using curl. Here’s the code to do so:

    $current_page_url =  "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    $fb_count_like_url = "http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27".$fb_count_like_url."%27";
    $fb_count_like_call = curl_init($fb_count_like_url);
    curl_setopt($fb_count_like_call,CURLOPT_RETURNTRANSFER,true);
    $fb_count_like_response = curl_exec($fb_count_like_call);

  • Got the number of Facebook likes for the current page by parsing the XML response from the previous step. (The XML response is stored in $fb_count_like_response)

  • Stored the number of Facebook likes and the page information in a table called jos_facebook_page_count_likes in the Joomla database.

Now that we have all this information stored, we were able to get the top 10 liked pages by issuing the following SQL query:

SELECT * FROM `jos_facebook_page_count_likes` ORDER BY number_of_facebook_likes DESC LIMIT 0, 10;

The above query was used in the module to list all the pages (of course, you might need to modify the above query to ensure that the pages returned are still published on your website).

If you want to develop a similar functionality on your website but think that it’s a bit complicated then just contact us and you can rest assured that we’ll do it for you in no time. Our fees are affordable and we are fast, efficient, and very friendly.

Note: We have noticed that the number of likes on Facebook expires. So, if someone liked a page on your website a year ago or so, his like won’t be counted in the end result.

No comments yet.

Leave a comment