function currency_api_convert in Currency 5
Same name and namespace in other branches
- 6 currency_api/currency_api.module \currency_api_convert()
- 7 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_exchange($from, $to, $amt); if ($ret['status'] == FALSE) { drupal_set_message(t('An error occured: '). $ret['message']); } else { print $amt .' '. $from .' = '. $ret['value']; }
Parameters
$currency_from: Currency to convert from
$currency_to: Currency to convert to
$amount: Option amount to convert. If not supplied, 1 is assumed.
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['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")
1 call to currency_api_convert()
File
- currency_api/
currency_api.module, line 95
Code
function currency_api_convert($currency_from, $currency_to, $amount = 1) {
$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['date'] = NULL;
$result['time'] = NULL;
$from = strtoupper($currency_from);
$to = strtoupper($currency_to);
if ($from == $to) {
return array(
'status' => TRUE,
'message' => 'success',
'value' => $amount,
'rate' => 1.0,
'date' => date('n/j/Y'),
'time' => date('g:ia'),
);
}
// load cached rate, if exists
$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 = "currency: Invalid currency_from={$from}";
_log_to_watchdog($msg, WATCHDOG_ERROR);
$result['message'] = $msg;
$result['status'] = FALSE;
}
if (FALSE == currency_api_get_desc($to)) {
$msg = "currency: Invalid currency_to={$to}";
_log_to_watchdog($msg, WATCHDOG_ERROR);
return FALSE;
$result['message'] = $msg;
$result['status'] = FALSE;
}
if (!is_numeric($amount)) {
$msg = "currency: Invalid amount={$amount}";
_log_to_watchdog($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,
));
_log_to_watchdog($msg, WATCHDOG_ERROR);
return FALSE;
}
if ($http_result->code != 200) {
$msg = t('currency: drupal_http_request code: @code', array(
'@code' => $http_result->code,
));
_log_to_watchdog($msg, WATCHDOG_ERROR);
return FALSE;
}
$record = $http_result->data;
}
if (!$record) {
$msg = 'currency: cannot contact Yahoo Finance host';
_log_to_watchdog($msg, WATCHDOG_ERROR);
$result['status'] = FALSE;
$result['message'] = $msg;
}
$currency_data = explode(',', $record);
$rate = $currency_data[1];
$date = $currency_data[2];
$time = $currency_data[3];
// Calculate the result
$value = $amount * $rate;
// Log it
_log_to_watchdog("currency: {$amount} {$from} = {$value} {$to}", WATCHDOG_NOTICE);
// Got what we need
$result['value'] = $value;
$result['rate'] = $rate;
$result['date'] = $date;
$result['time'] = $time;
$result['status'] = TRUE;
$result['message'] = 'success';
if (!$cached && variable_get('currency_api_fetch', UPDATE_FREQUENCY)) {
// cache rate does not exist, save it
currency_api_save($currency_from, $currency_to, $rate);
}
return $result;
}