Computer Nerd Kev


Projects > Currency Conv.

A PHP Currency Converter Function for Australian Dollar Input

While developing the OmberTech online store, I needed a currency conversion system to try to make all those strange foreign sorts who might drift in feel a little more at home. Although set for a wide variety of other, often far more specific, applications, PHP itself does not offer any built-in currency conversion functions. A few web searches suggested the very clumsy method of writing code to submit each conversion request to one of the currency converter web pages run by the likes of Google and Yahoo, then scraping the result from the returned page and popping it into your own page just as its user begins to wonder why it's taking so long to load.

Luckily, I was also able to find a much more promising method available in the form of XML currency rate files made available online by various institutions and organisations across the globe. Some examples:

Living "down under", my purposes were likely to be best served from the website of the Reserve Bank of Australia (RBA). All that remained was to process the particularly long-winded XML file into something usable, hence the construction of this short, possibly sweet, PHP function:


//Reads currency conversion rates to AUD from RBA data in XML format
//Reserve Bank of Australia conversion rates XML link:
//By Kevin Koster, 2017. Free for any use.

//Lists RBA Currency conversions in array: $curarray[TARGETCURRENCY]
function currencylist($currencyfile)
  global $curarray;
  function startelement(&$parser, &$elname, &$attribute)
   global $nextiscurrency, $nextisvalue;
   if ($elname === "CB:VALUE") //Contents is the currency conversion rate
    $nextisvalue = TRUE;
   elseif ($elname === "CB:TARGETCURRENCY") //Contents is the target currency ID
    $nextiscurrency = TRUE;
  function endelement()

  function characterdata(&$parser, &$contents)
   global $nextiscurrency, $nextisvalue, $curarray;
    if ($nextisvalue)
     $curarray['x'] = (float)$contents;
     $nextisvalue = FALSE;
    elseif ($nextiscurrency)
     $curarray[$contents] = $curarray['x'];
     $nextiscurrency = FALSE;

  $xmlfile = file_get_contents($currencyfile);
  $currencyparser = xml_parser_create('UTF-8');
  xml_set_element_handler($currencyparser, 'startelement', 'endelement');
  xml_set_character_data_handler($currencyparser, 'characterdata');
  if (!xml_parse($currencyparser, $xmlfile, TRUE))
   error_log ( "Currency Converter: XML Error: " . xml_error_string(xml_get_error_code($currencyparser)) .
   ", at line: " . xml_get_current_line_number($currencyparser) );

  unset($curarray['x'], $curarray['XXX'], $curarray['XDR']); //Remove unwanted entries from array
  return $curarray;

The function simply returns an array with an element for each currency listed by the RBA (except for those unwanted ones "unset" at the end), with the value of each such element being the conversion rate. It uses the rather painful old xml_parser functions, and is compatible with at least PHP 4.3.7 and later.

For example then, to convert $6 AUD to all the currencies that the RBA thinks matter (a bit of an insight into to the Australian economy, by the way), one might do something like:

include 'function.currencylist.php';

$input = 6;
$currencyrates = currencylist("./data/rba_exchange_rates.xml");

foreach ($currencyrates as $currency => $rate)
 echo "$currency - " . ($rate * $input) . "\n";

The RBA updates their currency rates at around 4PM Australian Eastern Standard Time every Australian business day. I simply set a Cron job to automatically download the updated file some time later. I'm not really sure whether the update time changes for daylight savings or not.

Finally, I've since discovered that the PHP PEAR extensions repository contains a currency converter tool, Services_ExchangeRates, designed to work with published XML files. It can also convert between the currency rates provided from different sources (in different target currencies). At the moment though, there is no "driver" provided to handle the format of the RBA's XML files. With the above function providing a decent range of currency options with significantly less overall complexity, I'll be sticking with my own work.

A general purpose AUD currency converter page using this function is now available.


Content Copyright Kevin Koster 2017