获取链接的Facebook参与度计数

时间:2019-01-16 13:50:52

标签: php facebook facebook-graph-api

我在Facebook参与度方面遇到一些问题。我以前从未使用过此功能,因此请有所了解。显然,这就是我目前通过PHP通过

获得这些东西的方式。
function getNumberOfShares($url, $id) {

        $url_https = "https://graph.facebook.com/?fields=og_object{likes.summary(total_count).limit(0)},share&id=https://url.com/part/" . $url."/". $id;

        $fileContents = file_get_contents($url_https);
        $data_share = json_decode($fileContents, true);
        $total = $data_share['share']['share_count'];
        echo $fileContents;
        return $total;
    }

我尝试使用$ access_token进行操作,但是由于请求过多,导致我的权限被拒绝。我需要一种方法,可以使每个请求至少间隔50-100个链接。谢谢。

在研究后进行编辑

require_once("fb/src/Facebook/autoload.php");
$default = "111|11A122SAFB";
$fb = new Facebook\Facebook([
  'app_id' => '{111}',
  'app_secret' => '{11A122SAFB}',
  'default_access_token' => $default,
    'default_graph_version' => 'v2.3',
  ]);

$batch = [
    $fb->request('GET', 'https://example.com/part/slug/id&fields=og_object{engagement}'),
    $fb->request('GET', 'https://example.com/part/slug/id&fields=engagement'),
    ];

try {
    $responses = $fb->sendBatchRequest($batch);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

foreach ($responses as $key => $response) {
  if ($response->isError()) {
    $e = $response->getThrownException();
    echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
    echo '<p>Graph Said: ' . "\n\n";
    var_dump($e->getResponse());
  } else {
    echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
    echo "Response: " . $response->getBody() . "</p>\n\n";
    echo "<hr />\n\n";
  }
}

现在,对于批处理中的两个链接,我得到了这样的响应,并且它们具有不同的结尾:

(0) HTTP status code: 200
Response: {"id":"https:\/\/example.com\/part\/slug\/id&fields=og_object{engagement}"}

(1) HTTP status code: 200
Response: {"id":"https:\/\/example.com\/part\/slug\/id&fields=engagement"}

有人知道如何与此分享,喜欢,评论(参与)总和。谢谢!

1 个答案:

答案 0 :(得分:0)

我只需要更改:

https://example.com/part/slug/id&fields=og_object{engagement}

对此:

https://graph.facebook.com/?fields=og_object{engagement}&id=https://example.com/part/slug/id