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
  • 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: 35,618
    Posted Date: Wednesday, February 12, 2003
  • 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: 24,914
    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: 15,424
    Posted Date: Sunday, July 18, 2004
  • 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: 18,667
    Posted Date: Thursday, October 28, 2004
  • Stop automated form submission by using Image Verification
    This tutorial will show you how to stop automatred form submissions by generating a random image that must be verified before the form is submitted.
    Author: Phil Williams
    Views: 20,101
    Posted Date: Thursday, October 20, 2005
  • Complete site encryption!
    This tutorial will show you how to encrypt an entire site by recursively working though the folder structure encrypting all the cfm files and copying any non-cfm files whilst keeping the directory structure intact.
    Author: Phil Williams
    Views: 12,219
    Posted Date: Sunday, July 16, 2006
  • Simple and Efficient RecordSet Paging
    A simple and efficient method to page through hundred or even thousands of recordset result.
    Author: Phil Williams
    Views: 5,248
    Posted Date: Thursday, January 8, 2009
  • Backup Your MySQL Database with just ColdFusion Code

    This tutorial will show you how to quickly and easily backup and restore a MySQL database using just ColdFusion scripts and avoiding having to use CFEXECUTE which is normally disallowed on shared hosting platforms.

    ColdFusion 8 has some handy tags that will provide database detail but what if you're running CF7? Normally you would have to resort to running the mysqldump command but this is restrictive if you wanted to provide your users with a method to backup their databases.


    Author: Phil Williams
    Views: 2,954
    Posted Date: Friday, July 10, 2009
Download the EasyCFM.COM Browser Toolbar!