function ResourceFetcher::fetchResource

Same name in other branches
  1. 9 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::fetchResource()
  2. 10 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::fetchResource()
  3. 11.x core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::fetchResource()

Overrides ResourceFetcherInterface::fetchResource

File

core/modules/media/src/OEmbed/ResourceFetcher.php, line 52

Class

ResourceFetcher
Fetches and caches oEmbed resources.

Namespace

Drupal\media\OEmbed

Code

public function fetchResource($url) {
    $cache_id = "media:oembed_resource:{$url}";
    $cached = $this->cacheGet($cache_id);
    if ($cached) {
        return $this->createResource($cached->data, $url);
    }
    try {
        $response = $this->httpClient
            ->get($url);
    } catch (RequestException $e) {
        throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
    }
    list($format) = $response->getHeader('Content-Type');
    $content = (string) $response->getBody();
    if (strstr($format, 'text/xml') || strstr($format, 'application/xml')) {
        $data = $this->parseResourceXml($content, $url);
    }
    elseif (strstr($format, 'text/javascript') || strstr($format, 'application/json')) {
        $data = Json::decode($content);
    }
    else {
        throw new ResourceException('The fetched resource did not have a valid Content-Type header.', $url);
    }
    $this->cacheSet($cache_id, $data);
    return $this->createResource($data, $url);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.