dongteng2534 2016-08-23 11:21
浏览 10
已采纳

如何计算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"

  • 写回答

2条回答 默认 最新

  • doqs8936 2016-08-23 12:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line