dsolwotv00116 2016-09-09 08:50
浏览 34
已采纳

检索Facebook页面像多个页面的计数

I have some code to get Facebook page likes:

<?php 
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('194583094853853845','APP ID|ACCESSCODE');    
?> 

The above method works; I can retrieve the page's like count from Facebook Page ID 194583094853853845.

I was given the task to allow the function to retrieve the like count of multiple pages. To that end, I changed the code so something like this:

  <?php 
    function fbLikeCount($id,$access_token) {
      //Request URL
        $retrievedID == 1633003770266238; // dynamic variable from another field $output["id"];
      $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($retrievedID,'APP ID|ACCESSCODE');                
    ?>

I use $retrievedID to pass the page ID which gets the data from another field / variable. $retrievedID may equal 1633003770266238 in one instance, while it may equal 1633456456456 for a different instance.

However, this doesn't seem to work. How can I fix it?

展开全部

  • 写回答

1条回答 默认 最新

  • duanjia2415 2016-09-09 10:53
    关注

    Now the count is not showing at all and gets the error.

    That is not how you report error in your code. Either write what it is returning as error or don't say that there is error at all.

    Pretty much, looking at your code the answer should be $retrievedID. Just look at where it is defined in the code you posted above.

    Remove it from the inside of fbLikeCount() function and move it before you call it in the main scope.

    function fbLikeCount($id, $access_token) {/* content */}
    $retrievedID == 1633003770266238; // dynamic variable from another field $output["id"];
    echo fbLikeCount($retrievedID,'APP ID|ACCESSCODE');
    

    That's everything I can see at the moment that is wrong with the code above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部