Currency Conversion using Yahoo Finance API

Just a quick and dirty PHP function to retrieve information from the Yahoo Finance API to perform a currency conversion
function ConvertCurrency($From,$To,$Amount){
$URL = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $From . $To .'=X';
$Handle = @fopen($URL, 'r');

if ($Handle) {
$Result = fgets($Handle, 4096);
fclose($Handle);
}
$allData = explode(',',$Result);
$Value = $allData[1];

return $Value * $Amount;
}

Usage:
$Amount = ConvertCurrency("NZD","USD",25);

2 thoughts on “Currency Conversion using Yahoo Finance API”

  1. Yahoo finance API is not available anymore. I have moved to MarketXLS after this change, much more reliable data.

Leave a Reply

Your email address will not be published. Required fields are marked *