function UrlResolver::discoverResourceUrl

Same name and namespace in other branches
  1. 10 core/modules/media/src/OEmbed/UrlResolver.php \Drupal\media\OEmbed\UrlResolver::discoverResourceUrl()
  2. 11.x core/modules/media/src/OEmbed/UrlResolver.php \Drupal\media\OEmbed\UrlResolver::discoverResourceUrl()
  3. 9 core/modules/media/src/OEmbed/UrlResolver.php \Drupal\media\OEmbed\UrlResolver::discoverResourceUrl()
  4. 8.9.x core/modules/media/src/OEmbed/UrlResolver.php \Drupal\media\OEmbed\UrlResolver::discoverResourceUrl()

Runs oEmbed discovery and returns the endpoint URL if successful.

Parameters

string $url: The resource's URL.

Return value

string|false URL of the oEmbed endpoint, or FALSE if the discovery was unsuccessful.

1 call to UrlResolver::discoverResourceUrl()
UrlResolver::getProviderByUrl in core/modules/media/src/OEmbed/UrlResolver.php
Tries to determine the oEmbed provider for a media asset URL.

File

core/modules/media/src/OEmbed/UrlResolver.php, line 49

Class

UrlResolver
Converts oEmbed media URLs into endpoint-specific resource URLs.

Namespace

Drupal\media\OEmbed

Code

protected function discoverResourceUrl($url) {
  $patterns = \array_map(static fn($hostPattern) => \sprintf('{%s}i', $hostPattern), Settings::get('media_oembed_discovery_trusted_host_patterns', []));
  $host = \parse_url($url, \PHP_URL_HOST);
  if (!$host) {
    return FALSE;
  }
  $allowed = FALSE;
  foreach ($patterns as $pattern) {
    if (\preg_match($pattern, $host)) {
      $allowed = TRUE;
      break;

    }
  }
  if (!$allowed) {
    return FALSE;
  }
  try {
    $response = $this->httpClient
      ->get($url);
  } catch (ClientExceptionInterface) {
    return FALSE;
  }
  // Only care about HTML responses.
  $response_type = strtolower($response->getHeaderLine('Content-Type'));
  if (!str_contains($response_type, 'text/html')) {
    return FALSE;
  }
  $document = Html::load((string) $response->getBody());
  $xpath = new \DOMXPath($document);
  return $this->findUrl($xpath, 'json') ?: $this->findUrl($xpath, 'xml');
}

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