function ResourceFetcher::parseResourceXml

Same name in other branches
  1. 8.9.x core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::parseResourceXml()
  2. 10 core/modules/media/src/OEmbed/ResourceFetcher.php \Drupal\media\OEmbed\ResourceFetcher::parseResourceXml()
  3. 11.x 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 224

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);
}

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