How to count likes on FB page?
facebook, facebook-like
Solution
As you already wrote in your question, you can query such information through Facebooks' Graph API. This short example will get the information of the Coca-Cola page, decode the JSON and outputs the number of people that like the page `$data->likes`.
<?php
$ch = curl_init("https://graph.facebook.com/CocaCola?access_token=<Access Token>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$raw = curl_exec($ch);
curl_close($ch);
$data = json_decode($raw);
echo $data->likes . " people like Coca-Cola";
?>
If you need to perform more tasks than just getting the likes of a page, consider using the Facebook SDK as cpilko suggested.
Problem
I have to do a very simple operation but my programming skills are not enough. I have to count likes in Facebook page and print that number on my web-site. I have two scripts that do the job well for ordinary web-sites, but they don't want to show the number of likes for the page. ``` <?php $source_url = "http://www.facebook.com/"; //This could be anything URL source including stripslashes($_POST['url']) $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url); $likes = $xml->link_stat->like_count; $comments = $xml->link_stat->comment_count; $total = $xml->link_stat->total_count; $max = max($shares,$likes,$comments); echo $likes; ?> <?php $fql = "SELECT url, normalized_url, share_count, like_count, comment_count, "; $fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM "; $fql .= "link_stat WHERE url = 'http://www.apple.com/'"; $apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql); $json=file_get_contents($apifql); print_r( json_decode($json)); ?> ``` Both scripts work for ordinary web-sites but cant fetch fb page likes number. May be I should enter the link in another format or something? I can get required data using graph like this http://graph.facebook.com/?ids=AutoSpecCenter , just by entering page name like that. But I don't know how to manipulate with this data.