Currency Conversion with Yahoo! Finance

How General Users and Programmers can Convert Currencies On-line

© Mark Alexander Bain

Oct 13, 2008
Yahoo! Finance Currency Conversion, Mark Alexander Bain
Yahoo! Finance is an exellent source of information such as stock quotes - but that's not all. This article looks at how to use Yahoo! Finance's currency conversion.

Everyone is interested in the stock markets at the moment and services such as Yahoo! Finance's stock quotes are a valuable resource for:

  • would be investors
  • stock market analysts
  • programmers wanting to use stock quotes in their own application

...and all of those people may be interested to learn that Yahoo! Finance also supplies a currency conversion service.

Using the Yahoo! Finance Currency Conversion Web Page

Anyone who has used Yahoo! Finance stock quotes knows just how easy it is to obtain the information that they need, and the Yahoo! Finance Currency Converter is no different:

  • the Yahoo! Finance Currency Converter can be found at http://finance.yahoo.com/currency
  • the web page contains two drop down boxes:
    • a drop down box for the currency to convert from
    • a drop down box for the currency to convert to
  • a input box for the amount of money to be converted

and, of course, a button to set the conversion into motion.

Downloading Yahoo! Finance Currency Conversion Data

The Yahoo! Finance currency conversion web page contains a link which, when clicked on, will load the data into a spreadsheet (such as Microsoft Excel or OpenOffice.org Calc); however (and this is where programmers may prick up their ears) the currency conversion data may also be accessed directly via the correct url. For example, to find the conversion rate from U.S. dollar to British pounds the url would be:

http://quote.yahoo.com/d/quotes.csv?s=USDGBP=X&f=l1&e=.csv

The above means : "convert from U.S. dollars(USD) to British pounds (GBP) using the most current conversion rate (l1) and output in csv format"; and armed with this information the programmer will be ready to start incorporating the data into an application - for example a PHP script.

Using Yahoo! Finance Currency Conversion Data in a PHP Script

A very simple PHP script is all that it takes to obtain the conversion rate, one that will format the url and then read the stream of data returned from the Yahoo! web site:

function show_currency_conversion ($currency_from, $currency_to) {
$url = "http://quote.yahoo.com/d/quotes.csv?s="
. $currency_from . $currency_to . "=X"
. "&f=l1&e=.csv";
open = open($url, "r");
$exchange_rate = freed(open, 2000);
close(open);
echo $exchange_rate;
}

If this function is saved into a PHP library (for example yahoo_finance.php) then it can be called from any PHP web page:

<?php
include ("yahoo_finance.php");
$currency_from=$_REQUEST['currency_from'];
$currency_to=$_REQUEST['currency_to'];
show_currency_conversion ($currency_from, $currency_to);
?>

And if this script is saved as yahoo_currency.php then it may be tested by calling it via a web browser, for example:

http://<my server>/yahoo_currency.php?currency_from=USD&currency_to=GBP

And then, of course, this PHP script can be used in an Ajax application.

Using Yahoo! Finance Currency Conversion Data in an Ajax Application

Once the PHP script is in place then that can be used as part of an Ajax application, for example one that allows the user to select currencies with radio buttons and then clicking a button to view the conversion rate:

<script type = "text/javascript">
var XMLHttp;
/Create the correct XML Http object for the browser/
if (navigator.appName == "Microsoft Internet Explorer") {
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
XMLHttp = new XMLHttpRequest();
}
function get_currency() {
var currency_form = document.forms['currency_form'];
/* Find out how many elements are in each set of radio buttons */
var from_length = currency_form.currency_from.length
var to_length = currency_form.currency_to.length
var currency_from = ""
var currency_to = ""
/* Find out which currencies have been selected */
for (var i = 0; i <from_length; i++) {
if (currency_form.currency_from[i].checked) {
currency_from = currency_form.currency_from[i].value
}
}
for (var i = 0; i <from_length; i++) {
if (currency_form.currency_to[i].checked) {
currency_to = currency_form.currency_to[i].value
}
}
/* If no currencies are selected then tell the user */
if ( currency_from == "" || currency_to == "") {
alert ("Please select a currency")
} else {
/* Send the request to the server */
var url = "yahoo_currency.php?"
+ "currency_from=" + currency_from
+ "&currency_to=" + currency_to
XMLHttp.open("GET", url, true);
XMLHttp.onreadystatechange = function() {
if (XMLHttp.readyState == 4) {
/* Once the server has completed its tasks display the result */
var response =
"1 " + currency_from + " = " + XMLHttp.responseText + " " + currency_to
document.getElementById( 'result_area' ).innerHTML = response
}
XMLHttp.send(null);
}
}
}
</script>

As well as the Javascript code an HTML form will be required - one that defines the radio buttons for the currencies and that has a button that will call the above Javascript function:

<form name=currency_form>
<table>
<tr><th>From</th><th>To</th></tr>
<tr><td>
<input type=radio name=currency_from value=EUr>euro<br />
<input type=radio name=currency_from value=GBp>british Pound<br />
<input type=radio name=currency_from value=USD>U.S. Dollar<br />
</td>
<td>
<input type=radio name=currency_to value=EUr>euro<br />
<input type=radio name=currency_to value=GBp>british Pound<br />
<input type=radio name=currency_to value=USD>U.S. Dollar<br />
</td>
<td>
<input type=button value="Do Currency Conversion"
onclick = "javascript:get_currency();">
</td>
<td>
<div id=result_area></div>
</td>
</tr>
</table>
</form>

Conclusion

With Yahoo! Finance anyone can easily access the on-line currency converter or, with a little more effort, a programmer can use that data in their own applications - all through the power of Javascript and PHP.

Additional Reading

Accessing Yahoo! Finance from PHP

Using Yahoo! Financial Stock Quotes

An Introduction to Google Finance


The copyright of the article Currency Conversion with Yahoo! Finance in AJAX Programming is owned by Mark Alexander Bain. Permission to republish Currency Conversion with Yahoo! Finance in print or online must be granted by the author in writing.


Yahoo! Finance Currency Conversion, Mark Alexander Bain
The Yahoo! Finance Currency Converter is On-line, Mark Alexander Bain
Yahoo! Finance Data can be used in an Ajax , Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Post Your Comment
NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
What is 6+1? Incorrect, please resolve x + y!