从页面 Post 获得 'total_count' 赞 'summary' - Facebook PHP SDK(已关闭)

Getting 'total_count' from Page Post Likes 'summary' - Facebook PHP SDK (CLOSED)

我是 PHP 和 Facebook PHP SDK 的新手,我希望从 'likes' 'summary' 中获取 'like_count' Facebook 页面 post。我当前的代码包含以下内容:

$response = $fb->get('/me/posts?fields=admin_creator,likes.limit(0).summary(true)&limit=30');
$getLikeCount = $response->getGraphEdge()->asArray();
foreach($getLikeCount as $likekey){
    if(isset($likekey['likes'])){
        var_export($likekey['likes']);
        foreach ($likekey['likes'] as $likekey){
            //echo $likekey['total_count'] . '<br>';
        }
    }
}

var_export($likekey['likes']); 导出空白数组,而 var_export($likekey['summary']); return 为 NULL。但是,在 Graph API Explorer 中,它 return 如下:

{
      "admin_creator": {
        "name": "NAME",
        "id": "ID"
      },
      "id": "ID",
      "likes": {
        "data": [
        ],
        "summary": {
          "total_count": 1022,
          "can_like": true,
          "has_liked": false
        }
      }
    },

如何访问 'total_count' 字段,因为通过我的 'likes' 和 'summary' 方法访问它不起作用。

编辑: 使用 getGraphEdge()->asArray(); 将不起作用,因为它没有 return 摘要数组。我会以某种方式必须从 getDecodedBody(); 或其他方法中获取值。如果我使用 $getLikeCount = $response->getDecodedBody();,代码如下:

foreach($getLikeCount as $key){
    if(isset($key['admin_creator'])){
        echo $key['admin_creator']['name'];
    }
}

它没有 return 任何东西。我以 'admin_creator' 为例,因为它在 $getLikeCount = $response->getGraphEdge()->asArray(); 时有效,但在我当前的方法中不起作用,但是我不能使用此方法,因为我试图从 'total_count' 字段中获取post 'likes''summary''summary' 在使用 getGraphEdge() 方法时不显示在数组中,仅在使用 getDecodedBody(); 时显示。我想知道是否有办法从 getDecodedBody() 获取值,或者是否有解决方法从 summary.

获取 total_count 字段

答案: 答案可以在下面找到。

尝试

$getLikeCount['likes']['summary']['total_count']

我找到了解决方法。

解决方法需要找到 post ID,然后执行另一个请求以仅获取 post 的 likes 字段。

$response = $fb->get('/me/posts?fields=admin_creator,likes.limit(0).summary(true)&limit=30');
$getPostID = $response->getGraphEdge()->asArray();
foreach($getPostID as $IDKey){
    if(isset($IDKey['id'])){
        $currentPostID = $IDKey['id'];
        $likesResponse = $fb->get('/'.$currentPostID.'/likes?limit=0&summary=true');
        echo $currentPostID . '<br>'; //optional
        $getLikeCount = $likesResponse->getGraphEdge();
        $currentLikeCount = $getLikeCount->getTotalCount();
        echo $currentLikeCount . '<br>';
    }
}

您无需进行更多 API 调用即可找到此数据。 摘要数据隐藏在 likes 字段的元数据中。评论和反应也是一样。

$response = $fb->get('/me/posts?fields=id,likes.limit(0).summary(true)');
foreach ($reponse->getGraphEdge() as $graphNode) {
    // Some fields can have metadata
    $likesData =  $graphNode->getField('likes')->getMetaData();
    echo 'The total of likes for the post ID "' .  $graphNode->getField('id') . '" is: '. $likesData['summary']['total_count'];
}