duanlie7962 2017-06-08 15:31
浏览 42
已采纳

facebook使用PHP SDK打开图表计数份额

As from Facebook documentation I can get the count of shares for a web page calling the following url https://graph.facebook.com/?id=http://www.google.it, the returned data are the following:

{
   "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"
}

I want to get the same data using the PHP SDK using the following code:

//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();

Accessing the $graphObject I can retrieve the same data except the ones in "share" key, that are what I'm interested.

If I use the GRAPH API Explorer, I get the same info using the PHP SDK:

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

Is there any way to get the "shares" using the PHP SDK?

  • 写回答

1条回答 默认 最新

  • doushan6692 2017-06-08 22:29
    关注

    try this graph api explorer v2.8

    but you cannot get share field with graph api v2.9 because share field is deprecated for versions v2.9 and higher,or you can use engagement field like this graph api explorer v2.9

    for PHP SDK:

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

    then you will get something like this

    {
      "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"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?