Facebook图表使用PHP获取帖子的喜欢/评论计数



我尝试了很多方法来做到这一点,但都失败了......

我正在使用facebook php sdk来创建我自己的新闻提要(提要来自页面的时间线)

为了在Facebook上获得喜欢,我使用$post['likes']其中喜欢会输出:

"data": [
           {
              "id": "100001919135377",
              "name": "Person name"
           },
           {
              "id": "1153253855",
              "name": "Person name"
           },
           {
              "id": "100000945245573",
              "name": "Person name"
           },
           {
              "id": "100002595937528",
              "name": "Person name"
           },
           {
              "id": "100001873157306",
              "name": "Person name"
           },
           {
              "id": "1356273210",
              "name": "Person name"
           }
        ]

我怎样才能计算点赞的数量,然后附和它?

评论也是如此,我使用 $post['评论'],它会输出

"data": [
           {
              "id": "637555672952364_6191387",
              "from": {
                 "name": "Person Name",
                 "id": "1153253855"
              },
              "message": "Comment content",
              "can_remove": false,
              "created_time": "2014-01-11T07:28:37+0000",
              "like_count": 0,
              "user_likes": false
           },
           {
              "id": "637555672952364_6191388",
              "from": {
                 "name": "Person Name",
                 "id": "1153253855"
              },
              "message": "Comment content",
              "can_remove": false,
              "created_time": "2014-01-11T07:28:39+0000",
              "like_count": 0,
              "user_likes": false
           }
        ]

同样的事情,我想计算评论的数量并回应它......

如果您需要了解更多信息,请告诉我。

提前致谢

使用 count(); 函数获取JSON数组具有的元素数

下面是一个简单的PHP示例,其中包含file_get_contents

<?php
function fetchUrl($url){
         return file_get_contents($url);
}
$authToken = "{Token}";
$json_object = fetchUrl("https://graph.facebook.com/{POST_ID}/likes?$authToken}&limit=5000"); // 
$feedarray   = json_decode($json_object, true);
$likesNum = count($feedarray['data']); // return the number of items in `data` array
print $likesNum;
?>

同样的事情也适用于评论

尝试在 api 调用中使用 comments.summary(true)

例如,在评论中添加计数

这是从Facebook获取类似count的面部bbok的简单方法

<?php
    function fetchUrl($url) {
        return file_get_contents($url);
    }
    $authToken = "{token}"; // Authentication Token
    $facebookUrl = "{page name only}"; // https://www.facebook.com/page name
    $json_object = fetchUrl("https://graph.facebook.com/$facebookUrl/?fields=fan_count&access_token=$authToken");
    $feedarray   = json_decode($json_object, true);
    echo number_format($feedarray['fan_count'],0,",","' "); // Will display count : 00'000
    echo $feedarray['fan_count']; // Will display count : 00000
 ?>
你可以

使用这个

$json_object = fetchUrl("https://graph.facebook.com/{POST_ID}/likes?field=total_count");

最新更新