The last article showed how to add a Facbook “Like” button to your website and observing the click event on the button to track and/or do something else with JavaScript when a user clicks on the “Like” button.
Now you might want to get some information on the URL via some automated processes like cronjobs etc. You can simply use FQL - the Facebook Query Language - for that purpose. FQL is similar to SQL but doesn’t support all of the features.
Using FQL
Here is some FQL to get some statistics for a link:
SELECT like_count, total_count, share_count, click_count from link_stat where url="http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/"
You can simply use a Facebook API client SDK or access the data directly via the URL even in your browser:
This will return XML like this:
<?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>4</like_count> <total_count>5</total_count> <share_count>1</share_count> <click_count>0</click_count> </link_stat> </fql_query_response>
This means you can also use something like cURL or even PHP’s file_get_contents() method to obtain this information and parse it with SimpleXML, DOM or even regular expressions.
Facebook doesn’t seem to define any specific API call limit but there seems to be a limit to the number of calls per day.
Pitfalls
The Facebook API doesn’t support “LIKE” queries in FQL so you cannot find out how many pages on your website were liked directly - only by querying like described above for every single URL on your website.
I hope this short article is useful for you. If you like it why not just click on the “Like” button below?
































Hi Sascha, that is very helpful information.
I have a question:
Are we limited to the count only?
Is there a way to know who (facebookID) liked what (url or xid)?
Just saw that you already mentioned the LIKE problem.
Also with substr it is not working
Like this … where substr(url,0,28) = “http://www.saschakimmel.com/”
https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20substr%28url,0,28%29%20=%20%22http://www.saschakimmel.com/%22
@Guvenc: Thanks for pointing this out, I actually tested this as well but as it didn’t work I didn’t care to mention it. Sorry if that lead you to spend time on researching it yourself.
thank you, I was looking for something to show who likes, but I got useful link, that is the count of sharing and Like. \ /0/
I’ll search for the fans list though
Thanks a lot.
Hi Sascha - I have found your article incredibly helpful - thank you so much.
Now I am sorry if this is a dumb question:
When I view the URL you supplied
https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22
I can see the results fine.
However - when I have tried to access it from scrips I get a blank result. EG I have tried simpleXML, curl, file_get_contents. I dont get errors as such… just seems like there is nothing retrurned. I have tested other XMLs, and they all load (although all are http rather than https). Is there something silly and simple I may have overlooked?
Thanks in advance
Mike
@Mike: Have you tried using http instead of https? It works as well. The only reason I see why it wouldn’t work might be a lack of (Open)SSL support in your PHP version or SSL certificate problems (especially with curl). However you should see an error or a warning. Have you made sure that errors are displayed?
ini_set('display_errors', true);error_reporting(E_ALL);
Hi Sascha: Thank you so much!!! That was the silly thing I overlooked. http worked just fine. I haven’t checked my open ssl support yet - but its likely off (new php installation).
Thanks again for the article, and your quick response!