【问题标题】:facebook open graph count shares using PHP SDKfacebook 使用 PHP SDK 打开图计数共享
【发布时间】:2017-06-08 15:31:15
【问题描述】:

从 Facebook 文档中我可以获取调用以下 url 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 获取“共享”?

【问题讨论】:

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


【解决方案1】:

试试这个 graph api explorer v2.8

但您无法使用 graph api v2.9 获取 share 字段,因为 v2.9 及更高版本不推荐使用 share 字段,或者您可以使用 engagement 字段,如 graph 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"
}

【讨论】:

  • 我正在接收一个对象而不是一个 json。此外,该对象没有任何关于 Graph Api Explorer 返回的信息。你能帮我说说为什么吗?非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多