diff --git a/.gitignore b/.gitignore index 57f6a194..eaaf4e2c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ vendor composer.lock phpunit.xml env.php -*.cache \ No newline at end of file +*.cache +.idea/ diff --git a/src/OEmbed.php b/src/OEmbed.php index 81936133..f6d88951 100644 --- a/src/OEmbed.php +++ b/src/OEmbed.php @@ -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()); @@ -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();