function emfield_request_xml in Embedded Media Field 6
Same name and namespace in other branches
- 5 emfield.module \emfield_request_xml()
- 6.3 deprecated/emfield-deprecated.inc \emfield_request_xml()
- 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. @TODO convert to simplexml
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... @param $return_errors optional; if TRUE and an error is encountered, a descriptive error array will be returned with elements for code, message and stat => 'error' @param $hash_extra optional; The key for caching is created from the arguments. If your provider does not use arguments (or uses the same arguments for each media item, you must pass a unique string as $hash_extra. Currently this is only used by bliptv and archive.org @param $serialized optional; Most uses of this function are expecting an XML file to be returned. However some providers (Flickr) can instead return a serialized PHP array. In this case set $serialized to TRUE. @param $json If TRUE, then the result will be a json encoded string. @return the xml results returned as an array
3 calls to emfield_request_xml()
- emvideo_archive_data in contrib/
emvideo/ providers/ archive.inc - hook emvideo_PROVIDER_data
- emvideo_myspace_data in contrib/
emvideo/ providers/ myspace.inc - hook emvideo_PROVIDER_data
- emvideo_vimeo_thumbnail in contrib/
emvideo/ providers/ vimeo.inc - hook emvideo_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 333 - Embedded Media Field is a CCK-based framework for 3rd party media files.
Code
function emfield_request_xml($provider, $url, $args = array(), $cached = TRUE, $return_errors = FALSE, $hash_extra = FALSE, $serialized = FALSE, $json = 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 $cache->data;
}
// connect and fetch a value
$result = drupal_http_request($url);
if (!empty($result->error)) {
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 ($json) {
$response = (array) json_decode($result->data);
}
else {
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--;
}
}
}
}
if ($cached) {
cache_set($arghash, $response, 'cache', time() + variable_get('emfield_cache_duration', 3600));
}
return $response;
}