Facebook API allows us to retrieve the statistics for “Like” button given a “liked” URL. The function get_facebook_like_button statistics returns xml result from facebook for a “liked” URL. This function utilizes CURL in PHP to retrieve the data from facebook. You should have CURL enable in order to use this function. To check if CURL is enable in apache, use phpinfo() function.
/*
* Return statistics of a facebook like button for a given URL.
* Data return is in XML format.
*
*/
function get_facebook_like_button_statistics($url)
{
$fb_url_query = "http://api.facebook.com/method/fql.query?query=";
$query = "SELECT like_count, total_count, share_count, click_count from link_stat where url='".$url."'";
$fb_url_query = $fb_url_query.urlencode($query);
$c = curl_init();
$timeout = 10;
curl_setopt($c,CURLOPT_URL,$fb_url_query);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($c);
curl_close($c);
return $data;
}Sample Code
$url = "http://sports.yahoo.com/mlb/blog/big_league_stew/post/TMI-Fan-8217-s-plan-to-propose-to-Ryan-Braun-b?urn=mlb-wp4407";
$data = get_facebook_like_button_statistics($url);
echo($data);Output
<?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>266</like_count>
<total_count>1629</total_count>
<share_count>944</share_count>
<click_count>0</click_count>
</link_stat>
</fql_query_response>You can use PHP xml parser to parse the data returned by facebook or use php DOM.
I was looking for a way to get facebook like button statistic and this is exactly what I need. Thanks.
Hello,
First of all thank you so much for providing the information about how to get FB likes statistics. However, I’ve something different requirement. In that just go to the following URL: http://www.facebook.com/uniquecolours You’ll see on right hand side “talking about this” count just beneath the likes count. I want to grab/fetch that count in PHP. Is that possible? Please help me on this. Thank you very much in advance and awaiting for you response asap.