Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ vendor
composer.lock
phpunit.xml
env.php
*.cache
*.cache
.idea/
31 changes: 28 additions & 3 deletions src/OEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ protected function fetchData(): array
return [];
}

$crawler = $this->extractor->getCrawler();
$request = $crawler->createRequest('GET', $this->endpoint);
$response = $crawler->sendRequest($request);
[$request, $response] = $this->makeRequest($this->endpoint);

if (self::isXML($request->getUri())) {
return $this->extractXML((string) $response->getBody());
Expand All @@ -49,6 +47,33 @@ protected function fetchData(): array
return $this->extractJSON((string) $response->getBody());
}

/**
* @param UriInterface|string $endpoint
* @return array
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
protected function makeRequest($endpoint)
{
$crawler = $this->extractor->getCrawler();
$request = $crawler->createRequest('GET', $endpoint);
$response = $crawler->sendRequest($request);

if ($response->getStatusCode() == 403) {
if (is_string($endpoint) && strpos('http://', $endpoint) === 0) {
$newEndpoint = str_replace('http://','https://', $endpoint);
}

if ($endpoint instanceof UriInterface && $endpoint->getScheme() == 'http') {
$newEndpoint = $endpoint->withScheme('https');
}
if (isset($newEndpoint)) {
return $this->makeRequest($newEndpoint);
}
}

return [$request, $response];
}

protected function detectEndpoint(): ?UriInterface
{
$document = $this->extractor->getDocument();
Expand Down