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 script
Here's the quick and dirty way:
<?php
$fb_id = 'AutoSpecCenter';
$url = 'https://graph.facebook.com/' . urlencode($fb_id);
$result = json_decode( file_get_contents($url) );
printf("<p>There are %s people who like %s</p>", $result->likes, $result->name);
You'd do much better off installing the Facebook PHP SDK or using cURL to get this.
You could set $fb_id
equal to a url as well.
This worked for me
<?php
function getData($username){
$access_token = 'YOUR ACCESS TOKEN'; //Replace it with your Access Token
$json = json_decode(file_get_contents("https://graph.facebook.com/".$username."?fields=fan_count&access_token=".$access_token),true);
return $json;
}
$data = getData("Mypage");//Replace it with your Username
$likes = $data['fan_count'];
echo "Facebook Likes: ".$likes;
?>
Try this code
<?php echo facebooklike('209414452444193');
function facebooklike($page_id){
$likes = 0; //Initialize the count
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
$likes = $json_output->likes;
}
return $likes;
}
function get_contents($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
I was having same problem, just adding likes.summary(true),comments.summary(true)
in parameter in against "fields" worked for me.
e.g. I used https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN&fields=story,from,story_tags,likes.summary(true),comments.summary(true)
instead of https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN
Also you can add other parameters if you want; separated by a ,
Also if you want count of single post you can use
https://graph.facebook.com/POST_ID/likes?summary=true&access_token=ACCESS_TOKEN
for likes count
Or
https://graph.facebook.com/POST_ID/comments?summary=true&access_token=ACCESS_TOKEN
for comment count
here is a easy way too....
<?php
$page_id = 'yourfbpageid'; // your facebook page id
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
$fans = $xml->page->fan_count;
?>
<li class="text white"><span></span><?php echo $fans.' Fans'; ?></li>
courtesy:ravi patel
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.