From 252cf358b2a6d38951e572f296c3f11e240c627f Mon Sep 17 00:00:00 2001 From: Dominik Jansen Date: Fri, 17 Apr 2020 15:50:01 +0200 Subject: [PATCH] make oembed configurable for additional oembed parameters --- CHANGELOG.md | 1 + README.md | 13 ++++++++++++- src/Extractor.php | 2 +- src/OEmbed.php | 9 ++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab80e91f..800c0296 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Ability to send settings to `CurlClient`. Added the `cookies_path` setting to customize the file used for cookies. #345 - `Document::selectCss()` function to select elements using css selectors instead xpath (it requires symfony/css-selector) - `Document::removeCss()` function to remove elements using css selectors instead xpath (it requires symfony/css-selector) +- Ability to configure OEmbed parameters from the outside ## 4.0.0 - 2020-03-13 Full library refactoring. diff --git a/README.md b/README.md index bd663d04..b536b311 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,17 @@ $oembed->int('width'); //Return the value as integer $oembed->url('url'); //Return the value as full url (converts relative urls to absolutes) ``` +Additional oEmbed parameters (like instagrams `hidecaption`) can also be provided: +```php +$embed = new Embed(); + +$result = $embed->get('https://www.instagram.com/p/B_C0wheCa4V/'); +$result->setSettings([ + 'oembed:query_parameters' => ['hidecaption' => true] +]); +$oembed = $info->getOEmbed(); +``` + ## LinkedData Another API available by default, used to extract info using the [JsonLD](https://www.w3.org/TR/json-ld/) schema. @@ -247,7 +258,7 @@ The `Extractor` class has many `Detectors`. Each detector is responsible to dete So, an adapter is basically an extractor created specifically for a site. It can contains also custom detectors or apis. If you see the `src/Adapters` folder you can see all adapters. -If you create an adapter, you need also register to Embed, so it knows in which website needs to use. To do that, there's the `ExtractorFactory` object, that is responsible for instantiate the right extractor for each site. +If you create an adapter, you need also register to Embed, so it knows in which website needs to use. To do that, there's the `ExtractorFactory` object, that is responsible for instantiate the right extractor for each site. ```php use Embed\Embed; diff --git a/src/Extractor.php b/src/Extractor.php index 76a74c47..9e48c2cf 100644 --- a/src/Extractor.php +++ b/src/Extractor.php @@ -129,7 +129,7 @@ public function getSettings(): array return $this->settings; } - public function getSetting(string $key): ?string + public function getSetting(string $key) { return $this->settings[$key] ?? null; } diff --git a/src/OEmbed.php b/src/OEmbed.php index 49e30846..13e1a0a8 100644 --- a/src/OEmbed.php +++ b/src/OEmbed.php @@ -22,6 +22,13 @@ private static function getProviders(): array return self::$providers; } + public function getOembedQueryParameters(string $url): array + { + $queryParameters = ['url' => $url, 'format' => 'json']; + + return array_merge($queryParameters, $this->extractor->getSetting('oembed:query_parameters') ?? []); + } + protected function fetchData(): array { $this->endpoint = $this->detectEndpoint(); @@ -63,7 +70,7 @@ private function detectEndpointFromProviders(): ?UriInterface return $this->extractor->getCrawler() ->createUri($endpoint) - ->withQuery(http_build_query(['url' => $url, 'format' => 'json'])); + ->withQuery(http_build_query($this->getOembedQueryParameters($url))); } private static function searchEndpoint(array $providers, string $url): ?string