Skip to content

Commit 48e3918

Browse files
committed
Facebook and Instagram adapters #392
1 parent 13f237e commit 48e3918

File tree

6 files changed

+126
-3
lines changed

6 files changed

+126
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,10 @@ If you need to pass settings to your detectors, you can use the `setSettings` me
331331
$info = $embed->get($url);
332332

333333
$info->setSettings([
334-
'oembed:query_parameters' => [] //Extra parameters send to oembed
335-
'twitch:parent' => 'example.com' //Required to embed twitch videos as iframe
334+
'oembed:query_parameters' => [], //Extra parameters send to oembed
335+
'twitch:parent' => 'example.com', //Required to embed twitch videos as iframe
336+
'facebook:token' => '1234|5678', //Required to embed content from Facebook
337+
'instagram:token' => '1234|5678', //Required to embed content from Instagram
336338
]);
337339
```
338340

src/Adapters/Facebook/Extractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(UriInterface $uri, RequestInterface $request, Respon
1515
{
1616
parent::__construct($uri, $request, $response, $crawler);
1717

18+
$this->oembed = new OEmbed($this);
1819
$this->title = new Detectors\Title($this);
1920
}
2021
}

src/Adapters/Facebook/OEmbed.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Facebook;
5+
6+
use Embed\OEmbed as Base;
7+
use Psr\Http\Message\UriInterface;
8+
9+
class OEmbed extends Base
10+
{
11+
const ENDPOINT_PAGE = "https://graph.facebook.com/v8.0/oembed_page";
12+
const ENDPOINT_POST = "https://graph.facebook.com/v8.0/oembed_post";
13+
const ENDPOINT_VIDEO = "https://graph.facebook.com/v8.0/oembed_url";
14+
15+
protected function detectEndpoint(): ?UriInterface
16+
{
17+
$token = $this->extractor->getSetting('facebook:token');
18+
19+
if (!$token) {
20+
return null;
21+
}
22+
23+
$uri = $this->extractor->getUri();
24+
$queryParameters = $this->getOembedQueryParameters((string) $uri);
25+
$queryParameters['access_token'] = $token;
26+
27+
return $this->extractor->getCrawler()
28+
->createUri($this->getEndpoint($uri->getPath()))
29+
->withQuery($queryParameters);
30+
}
31+
32+
private function getEndpoint(string $path): string
33+
{
34+
/* Videos
35+
https://www.facebook.com/{page-name}/videos/{video-id}/
36+
https://www.facebook.com/{username}/videos/{video-id}/
37+
https://www.facebook.com/video.php?id={video-id}
38+
https://www.facebook.com/video.php?v={video-id}
39+
*/
40+
if (strpos($path, '/video.php') === 0
41+
|| strpos($path, '/videos/') !== false
42+
) {
43+
return self::ENDPOINT_VIDEO;
44+
}
45+
46+
/* Posts
47+
https://www.facebook.com/{page-name}/posts/{post-id}
48+
https://www.facebook.com/{username}/posts/{post-id}
49+
https://www.facebook.com/{username}/activity/{activity-id}
50+
https://www.facebook.com/photo.php?fbid={photo-id}
51+
https://www.facebook.com/photos/{photo-id}
52+
https://www.facebook.com/permalink.php?story_fbid={post-id}
53+
https://www.facebook.com/media/set?set={set-id}
54+
https://www.facebook.com/questions/{question-id}
55+
https://www.facebook.com/notes/{username}/{note-url}/{note-id}
56+
*/
57+
if (strpos($path, '/photo.php') === 0
58+
|| strpos($path, '/photos/') === 0
59+
|| strpos($path, '/permalink.php') === 0
60+
|| strpos($path, '/media/') === 0
61+
|| strpos($path, '/questions/') === 0
62+
|| strpos($path, '/notes/') === 0
63+
|| strpos($path, '/posts/') !== false
64+
|| strpos($path, '/activity/') !== false
65+
) {
66+
return self::ENDPOINT_POST;
67+
}
68+
69+
return self::ENDPOINT_PAGE;
70+
}
71+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Instagram;
5+
6+
use Embed\Extractor as Base;
7+
use Embed\Http\Crawler;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\UriInterface;
11+
12+
class Extractor extends Base
13+
{
14+
public function __construct(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler)
15+
{
16+
parent::__construct($uri, $request, $response, $crawler);
17+
18+
$this->oembed = new OEmbed($this);
19+
}
20+
}

src/Adapters/Instagram/OEmbed.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Instagram;
5+
6+
use Embed\OEmbed as Base;
7+
use Psr\Http\Message\UriInterface;
8+
9+
class OEmbed extends Base
10+
{
11+
const ENDPOINT = "https://graph.facebook.com/v8.0/instagram_oembed";
12+
13+
protected function detectEndpoint(): ?UriInterface
14+
{
15+
$token = $this->extractor->getSetting('instagram:token');
16+
17+
if (!$token) {
18+
return null;
19+
}
20+
21+
$uri = $this->extractor->getUri();
22+
$queryParameters = $this->getOembedQueryParameters((string) $uri);
23+
$queryParameters['access_token'] = $token;
24+
25+
return $this->extractor->getCrawler()
26+
->createUri(self::ENDPOINT)
27+
->withQuery($queryParameters);
28+
}
29+
}

src/OEmbed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function fetchData(): array
4949
return $this->extractJSON((string) $response->getBody());
5050
}
5151

52-
private function detectEndpoint(): ?UriInterface
52+
protected function detectEndpoint(): ?UriInterface
5353
{
5454
$document = $this->extractor->getDocument();
5555

0 commit comments

Comments
 (0)