You are here

function oembed_default_callback in oEmbed 7

Same name and namespace in other branches
  1. 8 plugins/providers/default.inc \oembed_default_callback()
  2. 7.0 plugins/providers/default.inc \oembed_default_callback()

Default provider callback makes HTTP requests using drupal_http_request().

1 call to oembed_default_callback()
oembedembedly_provider_callback in modules/oembedembedly/plugins/providers/embedly.inc
1 string reference to 'oembed_default_callback'
default.inc in plugins/providers/default.inc

File

plugins/providers/default.inc, line 56

Code

function oembed_default_callback($plugin, $url, $matches, $parameters) {

  // Remote oEmbed endpoint request.
  $parameters['url'] = $url;
  $query = http_build_query($parameters, NULL, '&');
  $fetch_url = $plugin['endpoint'] . '?' . $query;

  //TODO: Add alternative ways of fetching the content - like http client?
  $response = drupal_http_request($fetch_url);
  if (isset($response->error)) {
    watchdog('oembed', 'Error fetching data from %url.', array(
      '%url' => $fetch_url,
    ), WATCHDOG_ERROR);
    throw new RuntimeException($response->error, $response->code);
  }

  // JSON or XML data might be returned, so be agnostic about decoding it.
  $embed = drupal_json_decode($response->data);

  // json_decode returns null when the input is unparseable.
  if (is_null($embed)) {
    try {
      $xml = @new SimpleXMLElement($response->data);
      $embed = array();
      foreach ($xml as $key => $value) {
        $embed[$key] = (string) $value;
      }
    } catch (Exception $e) {
      $msg = 'Could not parse response from %url.';
      if (function_exists('json_last_error')) {
        $msg .= ' json=' . json_last_error();
        if (function_exists('json_last_error_msg')) {
          $msg .= ' (' . json_last_error_msg() . ')';
        }
      }
      watchdog_exception('oembed', $e, $msg, array(
        '%url' => $fetch_url,
      ), WATCHDOG_ERROR);
      throw new RuntimeException(t($msg, array(
        '%url' => $fetch_url,
      )), WATCHDOG_ERROR, $e);
    }
  }
  if (!is_array($embed) || empty($embed['version']) || empty($embed['type']) || intval($embed['version']) != 1) {
    watchdog('oembed', 'Response from %url not a valid oEmbed response.', array(
      '%url' => $fetch_url,
    ), WATCHDOG_ERROR);
    throw new RuntimeException(t('Response from %url not a valid oEmbed response.', array(
      '%url' => $fetch_url,
    )), WATCHDOG_ERROR);
  }
  if (!isset($embed['title'])) {
    $embed['title'] = '';
  }
  return $embed;
}