Currency Conversion using Web Services

Currency Convertor Tutorial

Ever wanted to know how much something was in another currency? Now you can with this short and sweet piece of code?

This tutorial is very simple to implement and has endless uses. The biggest benefit will be to those running e-commerce stores who ship worldwide and want to get the latest exchange rates. The method uses Web Services to consume the latest exchange rate into your application. A web service is simply data that can be gathered and manipulated by your application.

The converter uses a free web service from www.xmethods.net which supplies literally thousands of different web services that you can just gobble up. For now though we are only interested in this one: http://www.xmethods.net/ve2/ViewListing.po?key=uuid:D784C184-99B2-DA25-ED45-3665D11A12E5

To get going, create a new page called exchange.cfm

Here's the code in full:

<form action="exchange.cfm?do" method="post" name="form">

Change this amount:

<input type="text" name="amount">

into

<select name="currency" size="1">

<option value="united states">US dollars</option>

<option value="euro">Euros</option>

</select>

<br><br>

<input type="submit" value="Convert it!">

</form>

<p>&nbsp;</p>

<cfif IsDefined("url.do")>

<cfinvoke

webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"

method="getRate"

returnvariable="aRate">

<cfinvokeargument name="country1" value="united kingdom"/>

<cfinvokeargument name="country2" value="#form.currency#"/>

</cfinvoke>

<cfoutput>

You entered <b>#form.amount#</b> which is worth #NumberFormat(Evaluate(amount * aRate), '.__')# in #form.currency# currency

</cfoutput>

</cfif>

OK lets break it down into chunks:

<form action="exchange.cfm?do" method="post" name="form">

Change this amount:

<input type="text" name="amount">

into

<select name="currency" size="1">

<option value="united states">US dollars</option>

<option value="euro">Euros</option>

</select>

<br><br>

<input type="submit" value="Convert it!">

</form>

This just sets up a simple form for you to enter the value you want to change and choose the currency you want to change it to. The full list of countries is show at the same location as the web service above.

<cfif IsDefined("url.do")>

<cfinvoke

webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"

method="getRate"

returnvariable="aRate">

<cfinvokeargument name="country1" value="united kingdom"/>

<cfinvokeargument name="country2" value="#form.currency#"/>

</cfinvoke>

OK the code here checks to see if the form has been submitted and calls the web service using the CFINVOKE tag. The two arguments are the currency you are converting from and to. The latter is set by our form.

<cfoutput>

You entered <b>#form.amount#</b> which is worth #NumberFormat(Evaluate(amount * aRate), '.__')# in #form.currency# currency

</cfoutput>

</cfif>

Finally we display the conversion by multiplying our entered amount by the exchange rate and format the number so we don't get loads of trailing zeros.

That's it! You can take this forward by adding more currencies, save the rate in a session var to stop multiple request, etc but for now you have a working currency converter!



All ColdFusion Tutorials By Author: Phil Williams
  • Create and email ZIP files on the fly!
    This tutorial will allow you to zip up a file or files on your server and email them to you. The whole tutorial runs to less than 20 lines of code!
    Author: Phil Williams
    Views: 9,014
    Posted Date: Thursday, October 28, 2004
  • Currency Conversion using Web Services
    A very simple currency convertor that uses the latest exchange rates through the available Web Service
    Author: Phil Williams
    Views: 11,950
    Posted Date: Wednesday, April 21, 2004
  • Remote Reboots with ColdFusion MX
    This tutorial will tell you how to reboot your server without even logging in.
    Author: Phil Williams
    Views: 7,096
    Posted Date: Sunday, July 18, 2004
  • SE Friendly URL's
    Get the search engines to really dig deep into your site by replacing the ? and & in your dynamic URL's with /. Tricks the SE bot into thinking it's a regular folder and eats up your content!
    Author: Phil Williams
    Views: 20,790
    Posted Date: Wednesday, February 12, 2003