function flickr_request in Flickr 7
Same name and namespace in other branches
- 5 flickr.inc \flickr_request()
- 6 flickr.inc \flickr_request()
Submit a request to Flickr.
Parameters
string $method: String method name.
string $args: Associative array of arguments names and values.
string $cacheable: Boolean indicating if it's safe cache the results of this request.
string $return_errors: Boolean indicating if the caller will handle displaying error messages.
Return value
array an array with the the result of the request, or FALSE on error.
20 calls to flickr_request()
- flickr_favorites_getpubliclist in ./
flickr.api.inc - Returns a list of favorite public photos for the given user.
- flickr_galleries_getinfo in ./
flickr.api.inc - Returns info about a given gallery.
- flickr_galleries_getlist in ./
flickr.api.inc - Returns the galleries curated by the specified user.
- flickr_galleries_getphotos in ./
flickr.api.inc - Returns a list of photos for a given gallery.
- flickr_groups_getinfo in ./
flickr.api.inc - Returns info about a given group.
File
- ./
flickr.inc, line 93 - The Flickr API functions.
Code
function flickr_request($method, $args, $cacheable = TRUE, $return_errors = FALSE) {
// Add in additional parameters then sort them for signing.
$args['api_key'] = trim(variable_get('flickr_api_key', ''));
$args['method'] = $method;
$args['format'] = 'json';
$args['nojsoncallback'] = 1;
ksort($args);
// Build an argument hash API signing (we'll also use it for the cache id).
$arg_hash = '';
foreach ($args as $k => $v) {
$arg_hash .= $k . $v;
}
// If we've got a secret, sign the arguments.
if ($secret = trim(variable_get('flickr_api_secret', ''))) {
$args['api_sig'] = md5($secret . $arg_hash);
}
// Build the URL.
foreach ($args as $k => $v) {
$encoded_params[] = urlencode($k) . '=' . urlencode($v);
}
$url = FLICKR_REST_ENDPOINT . '?' . implode('&', $encoded_params);
// If it's a cachable request, try to load a cached value.
if ($cacheable) {
if ($cache = cache_get("flickr_{$arg_hash}", 'cache')) {
// Check that the value is still "fresh".
if ($cache->expire > time()) {
return json_decode($cache->data, TRUE);
}
}
}
// If a cached value wasn't suitable, attempt to connect and fetch a result.
$cmethod = 'none';
$result = new stdClass();
$result->code = 0;
if ((variable_get('flickr_curl2', 0) || !function_exists('stream_socket_client')) && function_exists('curl_version')) {
$result = flickr_curl_http_request($url);
$cmethod = 'cURL';
}
elseif (function_exists('stream_socket_client')) {
$result = drupal_http_request($url);
$cmethod = 'stream_socket_client';
}
if ($result->code != 200 && ($cmethod == 'stream_socket_client' || $cmethod == 'none') && function_exists('curl_version')) {
// Try to use cURL when drupal_http_request returns a different code than
// 200 (valid request, no errors). Most likely are 403 (forbidden) or 408
// (Request Timeout).
$result = flickr_curl_http_request($url);
$cmethod = 'cURL';
$message = t('Automatic fallback to the cURL connection method kicked in to handle the request. Result code from the failing request') . ': ' . $result->code;
drupal_set_message($message, 'warning', FALSE);
watchdog('flickr', $message, array(), WATCHDOG_WARNING);
if ($return_errors && $result->code != 200) {
// Debug info.
if (variable_get('flickr_debug', 0) == 2 && function_exists('dpm')) {
dpm("Value of error 'result' in 'function flickr_request()' with connection method " . "'" . $cmethod . "' in 'flickr.inc':");
dpm($result);
}
return array(
'stat' => 'error',
// In Drupal <= 5.1, only HTTP errors are stored in $result->code
// correctly, not TCP/IP errors. We can not count on this variable being
// correct until this module requires Drupal 5.2 or above.
'code' => $result->code,
'message' => $result->error,
);
}
// Even the cURL method returns an error.
if ($result->code != 200) {
// Debug info.
if (variable_get('flickr_debug', 0) == 2 && function_exists('dpm')) {
dpm("Value of 'result' with error in 'function flickr_request()' with connection method " . "'" . $cmethod . "' in 'flickr.inc':");
dpm($result);
}
flickr_set_error(t("Could not connect to Flickr, Error: @error", array(
'@error' => $result->error,
)));
return FALSE;
}
}
if ($result->code == 0) {
$message = t("There seems to be no connection method available on your server. Neither 'stream_socket_client' nor 'cURL'.");
drupal_set_message($message, 'error', FALSE);
watchdog('flickr', $message, array(), WATCHDOG_ERROR);
}
// Debug info.
if (variable_get('flickr_debug', 0) == 2 && function_exists('dpm')) {
dpm("Value of 'result' in 'function flickr_request()' with the " . "'" . $cmethod . "' connection method in 'flickr.inc':");
dpm($result);
}
// Make sure it decodes.
$response = json_decode($result->data, TRUE);
if (!$response) {
if ($return_errors) {
return array(
'stat' => t('error'),
'code' => '-1',
'message' => t('The response was corrupted. It could not be decoded.'),
);
}
flickr_set_error(t("Flickr's response was corrupted and could not be decoded."));
return FALSE;
}
// Check that the request was successful.
if (flickr_response_has_error($response)) {
if ($return_errors) {
return $response;
}
flickr_set_error($response);
return FALSE;
}
elseif (isset($args) && variable_get('flickr_debug', 0)) {
$args['format'] = 'rest';
unset($args['api_sig']);
$attribs = urldecode(http_build_query($args, '', '&'));
$attribs = str_replace('"', '', $attribs);
$methodurl = l($method, 'https://www.flickr.com/services/api/' . $method . '.htm', array(
'attributes' => array(
'title' => t('Flickr Services: Flickr API') . ': ' . $method,
'target' => '_blank',
),
));
$explorerurl = l(t('API Explorer'), 'https://www.flickr.com/services/api/explore/' . $method, array(
'attributes' => array(
'title' => t('Flickr Api Explorer') . ' - ' . $method,
'target' => '_blank',
),
));
$requrl = l(t('following response'), FLICKR_REST_ENDPOINT . '?' . $attribs, array(
'attributes' => array(
'title' => t('Verify the passed arguments in the URL'),
'target' => '_blank',
),
));
$message = t("Debug info: Connection by %cmethod with method !methodurl gives the !requrl to check in the !explorerurl.", array(
'%cmethod' => $cmethod,
'!methodurl' => $methodurl,
'!explorerurl' => $explorerurl,
'!requrl' => $requrl,
)) . '<br />' . t("Passed arguments: %attribs", array(
'%attribs' => $attribs,
));
drupal_set_message($message, 'warning');
watchdog('flickr', $message, array(), WATCHDOG_WARNING);
}
// Save cacheable results for future use.
if ($cacheable) {
cache_set("flickr_{$arg_hash}", $result->data, 'cache', time() + variable_get('flickr_cache_duration', 3600));
}
return $response;
}