Skip to content

Commit

Permalink
Merge pull request #186 from richard67/update-fb-graph-api-14.0-and-c…
Browse files Browse the repository at this point in the history
…hange-field

Update Facebook service to Facebook Graph API version v18.0, change field from engagement to og_object
  • Loading branch information
digitalgopnik authored Oct 25, 2023
2 parents 74386de + b6e978d commit c055a2d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
12 changes: 3 additions & 9 deletions src/Backend/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setConfig(array $config): void
public function getRequest(string $url): RequestInterface
{
$accessToken = urlencode($this->config['app_id']) . '|' . urlencode($this->config['secret']);
$query = 'https://graph.facebook.com/v7.0/?id=' . urlencode($url) . '&fields=engagement&access_token='
$query = 'https://graph.facebook.com/v18.0/?id=' . urlencode($url) . '&fields=og_object%7Bengagement%7D&access_token='
. $accessToken;

return new \GuzzleHttp\Psr7\Request('GET', $query);
Expand All @@ -45,14 +45,8 @@ public function getRequest(string $url): RequestInterface
*/
public function extractCount(array $data): int
{
if (isset(
$data['engagement']['reaction_count'],
$data['engagement']['comment_count'],
$data['engagement']['share_count']
)) {
return $data['engagement']['reaction_count']
+ $data['engagement']['comment_count']
+ $data['engagement']['share_count'];
if (isset($data['og_object']['engagement']['count'])) {
return $data['og_object']['engagement']['count'];
}

return 0;
Expand Down
6 changes: 3 additions & 3 deletions tests/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testConfig()
$facebook->setConfig(['app_id' => 'foo', 'secret' => 'bar']);
$request = $facebook->getRequest('https://www.heise.de');
$this->assertEquals(
'id=' . urlencode('https://www.heise.de') . '&fields=engagement&access_token=foo%7Cbar',
'id=' . urlencode('https://www.heise.de') . '&fields=og_object%7Bengagement%7D&access_token=foo%7Cbar',
$request->getUri()->getQuery()
);
}
Expand All @@ -36,9 +36,9 @@ public function testUsesGraphApi()
$request = $facebook->getRequest('https://www.heise.de');

$this->assertEquals('graph.facebook.com', $request->getUri()->getHost());
$this->assertEquals('/v7.0/', $request->getUri()->getPath());
$this->assertEquals('/v18.0/', $request->getUri()->getPath());
$this->assertEquals(
'id=' . urlencode('https://www.heise.de') . '&fields=engagement&access_token=foo%7Cbar',
'id=' . urlencode('https://www.heise.de') . '&fields=og_object%7Bengagement%7D&access_token=foo%7Cbar',
$request->getUri()->getQuery()
);
}
Expand Down

0 comments on commit c055a2d

Please sign in to comment.