热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

facebook使用PHPSDK打开图表计数份额

如何解决《facebook使用PHPSDK打开图表计数份额》经验,为你挑选了1个好方法。

从Facebook文档中我可以获得一个网页的份额数,调用以下网址https://graph.facebook.com/?id=http://www.google.it,返回的数据如下:

{
   "share": {
      "comment_count": 0,
      "share_count": 636734
   },
   "og_object": {
      "id": "389545309239",
      "title": "Google",
      "type": "website",
      "updated_time": "2017-06-08T10:05:50+0000"
   },
   "id": "http://www.google.it"
}

我想使用以下代码使用PHP SDK获取相同的数据:

//require the Facebook PHP SDK

$client_id="my app id";
$client_secret="my secret key";
$default_access_token = file_get_contents( "https://graph.facebook.com/oauth/access_token?client_id=$client_id&client_secret=$client_secret&grant_type=client_credentials");

$OGRespOnse= new Facebook\Authentication\AccessToken($default_access_token);

$access_token = json_decode($OGResponse)->access_token;

$fb = new Facebook\Facebook([
    'app_id' => $client_id, // Replace {app-id} with your app id
    'app_secret' => $client_secret,
    'default_access_token' => $access_token,
]);

$helper = $fb->getRedirectLoginHelper();

$request = new \Facebook\FacebookRequest(
    $fb->getApp(),
    $access_token,
    'GET',
    '/',
    array(
        'id' => 'http://www.google.it',
    )
);

$respOnse= $fb->getClient()->sendRequest($request);

//$respOnse= $request->execute();
$graphObject = $response->getGraphObject();

访问$ graphObject我可以检索除"share"键之外的相同数据,这是我感兴趣的.

如果我使用GRAPH API Explorer,我会使用PHP SDK获得相同的信息:

{
  "og_object": {
  "id": "389545309239",
  "title": "Google",
  "type": "website",
  "updated_time": "2017-06-08T10:05:50+0000"
},
"id": "http://www.google.it"
} 

有没有办法使用PHP SDK获得"份额"?



1> Araqel Araqe..:

试试这个 图api explorer v2.8

但你无法使用图形api v2.9获得共享字段,因为版本v2.9及更高版本不推荐使用共享字段,或者你可以使用engagement像这样的字段api explorer v2.9

对于PHP SDK:

$request = new \Facebook\FacebookRequest(
    $fb->getApp(),
    $access_token,
    'GET',
    '/',
    array(
        'id'     => 'http://www.google.it',
        'fields' => 'engagement,og_object',
    )
);

然后你会得到这样的东西

{
  "engagement": {
    "reaction_count": 207037,
    "comment_count": 125335,
    "share_count": 304362,
    "comment_plugin_count": 0
  },
  "og_object": {
    "id": "389545309239",
    "title": "Google",
    "type": "website",
    "updated_time": "2017-06-08T15:58:56+0000"
  },
  "id": "http://www.google.it"
}


推荐阅读
author-avatar
喂╲偶稀飯妳
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有