Facebook就像计数网址

时间:2016-09-05 13:17:50

标签: php facebook facebook-graph-api facebook-like

随着Facebook改变他们的API并弃用旧的API,我需要获取有关单个页面的数据(如计数,共享计数,评论数)。

我想出了如何通过Facebook图表获取数据(示例链接):

https://graph.facebook.com/?fields=og_object{likes.limit(0).summary(true)},share&ids=http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9

但现在我不知道如何回应php中的单个数据(如计数)。我试过json,但没有成功:

$json = file_get_contents($xml);
$json_output = json_decode($json);

有关如何使这项工作的任何建议吗?

2 个答案:

答案 0 :(得分:1)

API Explorer会自动添加访问令牌,但您必须在URL中手动添加:

https://graph.facebook.com/?fields=og_object{likes.limit(0).summary(true)},share&ids=http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9&access_token=xxx

结果:

{
  "http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9": {
    "og_object": {
      "likes": {
        "data": [
        ],
        "summary": {
          "total_count": 0,
          "can_like": true,
          "has_liked": false
        }
      },
      "id": "949055545223224"
    },
    "share": {
      "comment_count": 0,
      "share_count": 346
    },
    "id": "http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9"
  }
}

答案 1 :(得分:0)

json_decode()的结果是Objects。 所以你可以像这样轻松浏览:

<?php    

$url = 'https://graph.facebook.com/?fields=og_object{likes.limit(0).summary(true)},share&ids=http://www.businessinsider.com/airlines-dont-disclose-carrier-fee-that-inflates-ticket-prices-2016-9';

$json = file_get_contents($url);
$json_output = json_decode($json);
foreach( $json_output as $site=>$data ){
    echo $site."\n";
    echo $data->og_object->likes->summary->total_count;
}

?>