如何计算fb喜欢的数量并在php的网页上显示

How to count number of f/b likes and show on a web page I have tried using access token but failed.

Any help would be appreciated!! Thank you

I've tried:

<?php $url = 'http://graph.facebook.com/app-id"';
echo '['.$url.']: '.json_decode(file_get_contents($url))->{'likes'}; ?>

And other codes.

But shows only this error:

"Trying to get property of non-object"

Try this code, just call this function with your page id and access_token

First Answer

<?php 
function fbLikeCount($id,$access_token){
    //Request URL
    $json_url ='https://graph.facebook.com/'.$id.'?fiel‌​ds=likes&access_token='.$access_token;
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);

    //Extract the likes count from the JSON object
    if($json_output->likes){
        return $likes = $json_output->likes;
    }else{
        return 0;
    }
}
//This Will return like count of Facebook page
echo fbLikeCount('page_id','access_token');
?>

Second Answer

function fbLikeCount($id,$access_token){
    //Request URL
    $json_url ='https://graph.facebook.com/'.$id.'?fields=fan_count&access_token='.$access_token;
    $json = file_get_contents($json_url);
    $json_output = json_decode($json);
    //Extract the likes count from the JSON object
    if($json_output->fan_count){
        return $likes = $json_output->fan_count;
    }else{
        return 0;
    }
}
echo fbLikeCount('page_id','app_id|app_secret');

I hope this will work for you.

You should use FB api to generate LikeBox "box_count". https://developers.facebook.com/docs/plugins/like-button

Include the JavaScript SDK on your page once, ideally right after the opening <body> tag.

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/pl_PL/sdk.js#xfbml=1&version=v2.7&appId=HERE-YOUR-APP-ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

Place this code wherever you want the plugin to appear on your page.

<div class="fb-like" data-href="https://developers.facebook.com/docs/plugins/" data-layout="box_count" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>

You have to generate app id and replace at step 1 with "HERE-YOUR-APP-ID" Here is example how to generate app id https://developers.facebook.com/docs/apps/register