How to count likes on FB page?

和自甴很熟 提交于 2019-12-09 03:23:14

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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;

?>



回答5:


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




回答6:


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;
    }
?>


来源:https://stackoverflow.com/questions/12147642/how-to-count-likes-on-fb-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!