Currency conversion API on a shoestring
Someone just came to our table at Mashed08 and asked if Yahoo! offers a currency conversion API. We don’t, but a few lines of PHP allows you to get the information from the Yahoo finance site:
function convert($from,$to){
$url= 'http://finance.yahoo.com/currency/convert?amt=1&from='.$from.'&to='.$to.'&submit=Convert';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
preg_match_all("/tabledata1\">([^<]+)<\/td>/",$feed,$cells);
return $cells[1][1];
}
echo convert(’USD’,'GBP’);
There’s a whole list of currency codes available on oanda.
A few more lines turns this into a JSON API:
header('Content-type:text/javascript');
$from = $_GET['from'];
$to = $_GET['to'];
$callback = $_GET['callback'];
if(preg_match("/[A-Z|a-z]{3}/",$to) && preg_match("/[A-Z|a-z]{3}/",$from)){
$to = strToUpper($to);
$from = strToUpper($from);
$url= ‘http://finance.yahoo.com/currency/convert?’ .
‘amt=1&from=’.$from.’&to=’.$to.’&submit=Convert’;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);
curl_close($ch);
preg_match_all("/tabledata1\">([^<]+)<\/td>/",$feed,$cells);
if(is_numeric($cells[1][1])){
$out = ‘{"from":"’.$from.’","to":"’.$to.’","factor":"’.$cells[1][1].’"}’;
} else {
$out = ‘{"error":"Could not convert currencies, are you sure about the names?"}’;
}
} else {
$out = ‘{"error":"Invalid Currency format, must be three letters"}’;
}
if(isset($callback)){
if(preg_match("/[a-z|A-Z|_|-|\$|0-9|\.]/",$callback)){
$out = $callback.’(’.$out.’)';
} else {
$out = ‘{"error":"Invalid callback method name"}’;
}
}
echo $out;
You have several parameters:
- from (mandatory): three letter currency code (upper or lower case)
- to (mandatory): three letter currency code (upper or lower case)
- callback (optional): the name of the callback method that should be wrapped around the resulting object
If something goes wrong, the API will return an object with an error property, otherwise you’ll get an object with three properties:
- from: the original currency
- to: the target currency
- factor: the conversion factor
Say you store this as convert.php somewhere, then you could do the following:
<script type="text/javascript">
function converted(obj){
if(obj.error){
alert(obj.error);
} else {
alert('one ' + obj.from + ' is ' + obj.factor + ' ' + obj.to);
}
}
</script>
<script type="text/javascript" src="convert.php?from=gbp&to=usd&callback=converted"></script>
This is a terrible dirty hack and if Yahoo finance ever changes their HTML (and they will), this will cease to work.
Tags: api, conversion, currency, finance, hacking, lackofapi, mashed08


June 21st, 2008 at 7:48 pm
Cheers Chris!!
June 23rd, 2008 at 9:45 am
This will be useful. Here’s an alternative technique I found.
http://twitter.com/mahemoff/statuses/810025299.
“There’s no JSON currencyservice, but you can make one by pointing Google Ajax feed API at currency RSS feeds (e.g. http://coinmill.com/rss/GBP_USD.xml)”
June 24th, 2008 at 9:14 am
Hey Chris,
Within character classes the pipe symbol is a literal character rather than an alternation operator so [A-Z|a-z] means match any upper or lower case letter or the character “|”. As a result you should be able to safely remove all of the pipes for your regex to still match.
July 14th, 2008 at 7:29 pm
> This is a terrible dirty hack and if Yahoo finance ever changes their HTML (and they will), this will cease to work.
You can use the following url instead: http://download.finance.yahoo.com/d/quotes.csv?s=USDGBP=X&f=sl1d1t1ba&e=.csv, which will get the data in .csv format and then you don’t need to parse any html. The third parameter (f) can be modified to your needs, it specifies the order of fields returned in the file.
July 21st, 2008 at 7:19 pm
I am using Firefox and get the Firebug error “missing name after . operator” for line 2 in the convert.php file. This is the line:
$url=’http://finance.yahoo.com/currency/convert?amt=1&from=’.$from.’&to=’.$to.’&submit=Convert’;
Any ideas what is wrong?
July 21st, 2008 at 10:19 pm
@stephen the code example has “smart quotes”, replace all of them with ‘ and that should do it.