You are here

function emfield_request_xml in Embedded Media Field 5

Same name and namespace in other branches
  1. 6.3 deprecated/emfield-deprecated.inc \emfield_request_xml()
  2. 6 emfield.module \emfield_request_xml()
  3. 6.2 emfield.module \emfield_request_xml()

When an include file requires to read an xml to receive information, such as for thumbnails, this script can be used to request the xml and return it as an array. i suspect it could be done easier (and more quickly) in php 5.

Parameters

$provider: the string of the third party provider, such as 'youtube', 'flikr', or 'google' @param $url the url for the xml request @param $args an array of args to pass to the xml url @param $cached optional; if TRUE, the result of this xml request will be cached. good to play nice w/ the third party folks so they don't stop providing service to your site... @return the xml results returned as an array

2 calls to emfield_request_xml()
video_cck_livestream_request in contrib/video_cck/providers/livestream.inc
this is a wrapper for video_cck_request_xml that includes livestream's api key
video_cck_vimeo_thumbnail in contrib/video_cck/providers/vimeo.inc
hook video_cck_PROVIDER_thumbnail returns the external url for a thumbnail of a specific video TODO: make the args: ($embed, $field, $item), with $field/$item provided if we need it, but otherwise simplifying things

File

./emfield.module, line 709

Code

function emfield_request_xml($provider, $url, $args = array(), $cached = TRUE, $return_errors = FALSE, $hash_extra = FALSE, $serialized = FALSE) {
  ksort($args);

  // build an argument hash that we'll use for the cache id and api signing
  $arghash = $provider . ':';
  foreach ($args as $k => $v) {
    $arghash .= $k . $v;
  }

  // build the url
  foreach ($args as $k => $v) {
    $encoded_params[] = urlencode($k) . '=' . urlencode($v);
  }
  if (!empty($encoded_params)) {
    $url .= '?' . implode('&', $encoded_params);
  }

  // some providers, such as bliptv, actually change the url, and not just the queries.
  // we provide an extra section for a unique identifier in that case
  if (isset($hash_extra)) {
    $arghash .= ':' . $hash_extra;
  }

  // if it's a cachable request, try to load a cached value
  if ($cached && ($cache = cache_get($arghash, 'cache'))) {
    return unserialize($cache->data);
  }

  // connect and fetch a value
  $result = drupal_http_request($url);
  if ($result->code != 200) {
    if ($return_errors) {
      return array(
        'stat' => 'error',
        'code' => $result->code,
        'message' => 'HTTP Error: ' . $result->error,
      );
    }
    emfield_set_error(t("Could not connect to @provider, HTTP error @error", array(
      '@error' => $result->code,
      '@provider' => $provider,
    )));
    return array();
  }
  if ($serialized) {

    // Flickr gives us a serialized php array. Make sure it unserializes.
    $response = unserialize($result->data);
    if (!$response) {
      if ($return_errors) {
        return array(
          'stat' => 'error',
          'code' => '-1',
          'message' => 'The response was corrupted, it could not be unserialized.',
        );
      }
      emfield_set_error(t("The response from @provider was corrupted and could not be unserialized.", array(
        '@provider' => $provider,
      )));
      return array();
    }
  }
  else {
    $parser = drupal_xml_parser_create($result->data);
    $vals = array();
    $index = array();
    xml_parse_into_struct($parser, $result->data, $vals, $index);
    xml_parser_free($parser);
    $response = array();
    $response['_emfield_arghash'] = $arghash;
    $level = array();
    $start_level = 1;
    foreach ($vals as $xml_elem) {
      if ($xml_elem['type'] == 'open') {
        if (array_key_exists('attributes', $xml_elem)) {
          list($level[$xml_elem['level']], $extra) = array_values($xml_elem['attributes']);
        }
        else {
          $level[$xml_elem['level']] = $xml_elem['tag'];
        }
      }
      if ($xml_elem['type'] == 'complete') {
        $php_stmt = '$response';
        while ($start_level < $xml_elem['level']) {
          $php_stmt .= '[$level[' . $start_level . ']]';
          $start_level++;
        }
        $php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];' . $php_stmt . '[$xml_elem[\'tag\']][] = $xml_elem[\'attributes\'];';
        eval($php_stmt);
        $start_level--;
      }
    }
  }
  cache_set($arghash, 'cache', serialize($response), time() + variable_get('emfield_cache_duration', 3600));
  return $response;
}