Friday, 2 April 2010

php exchange rate

Making multi currency websites can be a complete nightmare. Exchange rates change at a moments notice and it can be a constant struggle to keep yourself from losing money if one of the many markets crash.

Therefore I did a little bit of research and found you can get a rough idea of exchange rates by using yahoo finance. Yahoo provide a csv download from their site with live data. It's probably not as reliable as a feed from, but it's free and gives you a good indication of the exchange rate.

Here's a funscion i've made fo get this data;

function fget_exchange_rate($into, $infrom='GBP'){
$lcsv = file_get_contents(''.$infrom.$into.'=X&f=sl1d1t1ba&e=.csv');
$la = split(',',str_replace('"','',$lcsv));
if($la[1] != '') return $la[1];
return false;

echo('US $'.fget_exchange_rate('USD').'
echo('AU $'.fget_exchange_rate('AUD').'
echo('Euro '.fget_exchange_rate('EUR').'
echo('Japan '.fget_exchange_rate(JPY).'

You could add this to an automatic script to update the prices on your site daily. However, please be aware that you still need to keep an eye on things just incase you encounter problems.