Self executing function in JavaScript
Get live exchange rates in PHP
Update: Yahoo has since closed down this service. Below script will no longer work.
A very simple PHP script to get almost live currency exchange rates from Yahoo Finance. I’m saying "almost" as as far as I know Yahoo updates it with some minor delay.
$from = 'GBP';
$to = 'USD';
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
$handle = fopen($url, 'r');
if ($handle) {
$result = fgetcsv($handle);
fclose($handle);
}
echo '1 '.$from.' is worth '.$result[0].' '.$to.' Based on data on '.$result[1].' '.$result[2];
On the day of writing this post, above script would display:
1 GBP is worth 1.6125 USD Based on data on 11/8/2010 10:26am
Short explanation
I think everything is clear except for the URL and its parameters. Lets have a closer look.
In our example:
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
translates to
http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s=GBPUSD=X
Pasting this into your browser returns a csv file with three columns. Current exchange rate, date and time. Where:
- l1 – exchange rate,
- d1 – date,
- t1 – time
If exact time is not what you’re looking for and all you need is just the exchange rate, your url could look like this:
http://finance.yahoo.com/d/quotes.csv?f=l1&s=GBPUSD=X
I couldn’t find full list of parameters on yahoo but this site seems to have it all covered.
Yahoo finance not only gives you access to exchange rates but also stock data.