|
| 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 | +} |
0 commit comments