function currency_api_convert in Currency 7
Same name and namespace in other branches
- 5 currency_api/currency_api.module \currency_api_convert()
- 6 currency_api/currency_api.module \currency_api_convert()
Currency exchange rate API function.
This function converts two currencies using exchange rates from Yahoo Finance. The currency codes are standard ISO 3-letter codes, and you can find the details here: http://www.oanda.com/site/help/iso_code.shtml
Here is an example on how to use it:
$from = 'CAD'; $to = 'USD'; $amt = 20; $ret = currency_api_convert($from, $to, $amt); if ($ret['status'] == FALSE) { drupal_set_message(t('An error occured: '). $ret['message']); } else { print $amt . ' ' . $from . ' = ' . $ret['value'] . ' ' . $to; }
Parameters
$currency_from: Currency to convert from.
$currency_to: Currency to convert to.
$amount: (optional) Amount to convert. Defaults to 1.
$decimals: (optional) Number of digits to the right of the decimal point. Leave out this parameter if you want the actual currency result to proceess it yourself. Defaults to NULL.
Return value
$result An associative array that contains the following: $result['status'] - TRUE or FALSE $result['message'] - 'success' when status is TRUE, otherwise, contains a descriptive error text The following items are only returned when status is TRUE $result['value'] - $amount * exchange rate of $currency_from into $currency_to $result['rate'] - Exchange rate of $currency_from into $currency_to $result['timestamp'] - Timestamp of the last update to the rates $result['date'] - Date of the last update to the rates (Format is "m/d/yyyy") $result['time'] - Time of the last update to the rates (Format is "h:mmpm")
3 calls to currency_api_convert()
- currency_views_preload in includes/
views/ currency.views.inc - Calls currency_api_convert for every exchange rate that should be displayed according to filter and argument settings to ensure that rates are in the database when views performs it's query.
- theme_currency_result in ./
currency.module - Theme implementation for currency exchange result.
- _currency_api_filter_process in currency_api/
currency_api.module - Processes values for the currency api filter.
File
- currency_api/
currency_api.module, line 113 - This module provides an API for currency conversion.
Code
function currency_api_convert($currency_from, $currency_to, $amount = 1, $decimals = NULL) {
$currency_array = array(
's' => 'Currencies',
'l1' => 'Last',
'd1' => 'Date',
't1' => 'Time',
);
$result = array();
$result['status'] = FALSE;
$result['message'] = NULL;
$result['value'] = 0;
$result['rate'] = 1.0;
$result['timestamp'] = NULL;
$result['date'] = NULL;
$result['time'] = NULL;
$from = drupal_strtoupper($currency_from);
$to = drupal_strtoupper($currency_to);
if ($from == $to) {
return array(
'status' => TRUE,
'message' => 'success',
'value' => $amount,
'rate' => 1.0,
'timestamp' => REQUEST_TIME,
'date' => date('n/j/Y'),
'time' => date('g:ia'),
);
}
// Load cached rate, if exists.
$record = NULL;
$cached = currency_api_load($record, $currency_from, $currency_to);
if (!$record) {
// Cached rate not found, go get it.
$url = 'http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=' . currency_api_get_fields($currency_array) . '&s=' . $from . $to . '=X';
// Validate the passed currency codes, to make sure they are valid.
if (FALSE == currency_api_get_desc($from)) {
$msg = t("currency: Invalid currency_from = %from", array(
'%from' => $from,
));
currency_log($msg, WATCHDOG_ERROR);
$result['message'] = $msg;
$result['status'] = FALSE;
}
if (FALSE == currency_api_get_desc($to)) {
$msg = t("currency: Invalid currency_to = %to", array(
'%to' => $to,
));
currency_log($msg, WATCHDOG_ERROR);
return FALSE;
$result['message'] = $msg;
$result['status'] = FALSE;
}
if (!is_numeric($amount)) {
$msg = t("currency: Invalid amount = %amount", array(
'%amount' => $amount,
));
currency_log($msg, WATCHDOG_ERROR);
$result['message'] = $msg;
$result['status'] = FALSE;
}
$http_result = drupal_http_request($url);
if (isset($http_result->error)) {
$msg = t('currency: drupal_http_request error: @error', array(
'@error' => $http_result->error,
));
currency_log($msg, WATCHDOG_ERROR);
return FALSE;
}
if ($http_result->code != 200) {
$msg = t('currency: drupal_http_request code: @code', array(
'@code' => $http_result->code,
));
currency_log($msg, WATCHDOG_ERROR);
return FALSE;
}
$record = $http_result->data;
}
if (!$record) {
$msg = t('currency: cannot contact Yahoo Finance host');
currency_log($msg, WATCHDOG_ERROR);
$result['status'] = FALSE;
$result['message'] = $msg;
}
else {
$currency_data = explode(',', $record);
$rate = $currency_data[1];
$date = $currency_data[2];
$time = $currency_data[3];
$timestamp = strtotime(str_replace('"', '', $date) . ' ' . str_replace('"', '', $time));
// Calculate the result.
$value = $amount * $rate;
// Format the result if $decimals value was specified.
if ($decimals) {
if (module_exists('format_number')) {
$value = format_number($value, $decimals);
}
else {
$value = number_format($value, $decimals);
}
}
// Log it.
$msg = t("currency: @amount @from = @value @to", array(
'@amount' => $amount,
'@from' => $from,
'@value' => $value,
'@to' => $to,
));
currency_log($msg, WATCHDOG_NOTICE);
// Got what we need.
$result['value'] = $value;
$result['rate'] = $rate;
$result['timestamp'] = $timestamp;
$result['date'] = $date;
$result['time'] = $time;
$result['status'] = TRUE;
$result['message'] = 'success';
if (!$cached) {
// Cache rate does not exist, save it.
currency_api_save($currency_from, $currency_to, $rate);
}
}
return $result;
}