function emfield_request_xml in Embedded Media Field 6.2
Same name and namespace in other branches
- 5 emfield.module \emfield_request_xml()
- 6.3 deprecated/emfield-deprecated.inc \emfield_request_xml()
- 6 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.
Note that emfield_retrieve_xml() in most cases will be better, as it's a wrapper around simplexml.
Parameters
$provider: The string of the third party provider, such as 'youtube' or 'flickr'. @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 with 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.
File
- ./
emfield.module, line 370 - 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 to request XML: 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;
}