dsolwotv00116 2016-09-09 16: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 18: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.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备