Skip to content

Commit e8f6400

Browse files
committed
make oembed configurable for additional oembed parameters
1 parent f255552 commit e8f6400

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
- Ability to send settings to `CurlClient`. Added the `cookies_path` setting to customize the file used for cookies. #345
1111
- `Document::selectCss()` function to select elements using css selectors instead xpath (it requires symfony/css-selector)
1212
- `Document::removeCss()` function to remove elements using css selectors instead xpath (it requires symfony/css-selector)
13+
- Ability to configure OEmbed parameters from the outside
1314

1415
## 4.0.0 - 2020-03-13
1516
Full library refactoring.

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ $oembed->int('width'); //Return the value as integer
167167
$oembed->url('url'); //Return the value as full url (converts relative urls to absolutes)
168168
```
169169

170+
Additional oEmbed parameters (like instagrams `hidecaption`) can also be provided:
171+
```php
172+
$embed = new Embed();
173+
$info = $embed->get('https://www.instagram.com/p/B_C0wheCa4V/');
174+
$oembed = $info->getOEmbed();
175+
$oembed->setQueryParameters(['hidecaption' => true]);
176+
177+
$oembed->all();
178+
```
179+
170180
## LinkedData
171181

172182
Another API available by default, used to extract info using the [JsonLD](https://www.w3.org/TR/json-ld/) schema.
@@ -247,7 +257,7 @@ The `Extractor` class has many `Detectors`. Each detector is responsible to dete
247257

248258
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.
249259

250-
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.
260+
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.
251261

252262
```php
253263
use Embed\Embed;

src/OEmbed.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class OEmbed
1313

1414
private static $providers;
1515

16+
private array $queryParameters;
17+
1618
private static function getProviders(): array
1719
{
1820
if (!is_array(self::$providers)) {
@@ -22,6 +24,23 @@ private static function getProviders(): array
2224
return self::$providers;
2325
}
2426

27+
public function addQueryParameters(array $queryParameters): self
28+
{
29+
return $this->setQueryParameters(array_merge($this->getQueryParameters(), $queryParameters));
30+
}
31+
32+
public function setQueryParameters(array $queryParameters): self
33+
{
34+
$this->queryParameters = $queryParameters;
35+
36+
return $this;
37+
}
38+
39+
public function getQueryParameters(): array
40+
{
41+
return $this->queryParameters ?? [];
42+
}
43+
2544
protected function fetchData(): array
2645
{
2746
$this->endpoint = $this->detectEndpoint();
@@ -63,7 +82,7 @@ private function detectEndpointFromProviders(): ?UriInterface
6382

6483
return $this->extractor->getCrawler()
6584
->createUri($endpoint)
66-
->withQuery(http_build_query(['url' => $url, 'format' => 'json']));
85+
->withQuery(http_build_query(array_merge(['url' => $url, 'format' => 'json'], $this->getQueryParameters())));
6786
}
6887

6988
private static function searchEndpoint(array $providers, string $url): ?string

0 commit comments

Comments
 (0)