检索多个页面的 Facebook 页面点赞数

Retrieving the Facebook Page Like Count for multiple pages

我有一些代码可以让 Facebook 页面获得点赞:

<?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');    
?> 

以上方法有效;我可以从 Facebook 页面 ID 194583094853853845 检索页面的点赞数。

我的任务是允许函数检索多个页面的点赞数。为此,我更改了代码,如下所示:

  <?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');                
    ?>

我使用 $retrievedID 传递从另一个字段/变量获取数据的页面 ID。 $retrievedID 在一个实例中可能等于 1633003770266238,而在另一个实例中可能等于 1633456456456。

但是,这似乎不起作用。我该如何解决?

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

这不是您在代码中报告错误的方式。要么将返回的内容写为错误,要么根本不说有错误。

差不多,看看你的代码,答案应该是 $retrievedID。只需看看它在您上面发布的代码中的定义位置即可。

将其从 fbLikeCount() 函数内部移除并在主作用域中调用它之前移动它。

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

这就是我目前能看到的所有上面代码错误的地方。