You are here

protected function ResourceFetcher::parseResourceXml in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::parseResourceXml()
  2. 9 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::parseResourceXml()

Parses XML resource data.

Parameters

string $data: The raw XML for the resource.

string $url: The resource URL.

Return value

array The parsed resource data.

Throws

\Drupal\media\OEmbed\ResourceException If the resource data could not be parsed.

1 call to ResourceFetcher::parseResourceXml()
ResourceFetcher::fetchResource in core/modules/media/src/OEmbed/ResourceFetcher.php
Fetches an oEmbed resource.

File

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

Class

ResourceFetcher
Fetches and caches oEmbed resources.

Namespace

Drupal\media\OEmbed

Code

protected function parseResourceXml($data, $url) {

  // Enable userspace error handling.
  $was_using_internal_errors = libxml_use_internal_errors(TRUE);
  libxml_clear_errors();
  $content = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);

  // Restore the previous error handling behavior.
  libxml_use_internal_errors($was_using_internal_errors);
  $error = libxml_get_last_error();
  if ($error) {
    libxml_clear_errors();
    throw new ResourceException($error->message, $url);
  }
  elseif ($content === FALSE) {
    throw new ResourceException('The fetched resource could not be parsed.', $url);
  }

  // Convert XML to JSON so that the parsed resource has a consistent array
  // structure, regardless of any XML attributes or quirks of the XML parser.
  $data = Json::encode($content);
  return Json::decode($data);
}