Here is an easy and free way to add a currency converter to you application.
First, let’s define some variables.
<cfset from = 'EUR'> <cfset to = 'JPY'> <cfset amount = 1>
Then, let’s get an HTTP request to Google search page.
<cfhttp url="http://www.google.co.jp/search?hl=en&source=hp&q=#arguments.amount##arguments.from#+in+#arguments.to#" useragent="MOZILLA" method="GET" resolveurl="Yes" throwOnError="Yes"/>
Pretty simple stuff so far. If you actually go to that url, you’ll see the result that we want in bold on the Google result page. That’s what we want. As ColdFusion stores the response in cfhttp.FileContent, let’s get that.
<cfset c = cfhttp.FileContent>
Now, let’s use regex to extract the data we want.
<cfset temp = REMatch('<h2\b[^>]*><b>.+?</b></h2>',c)>
<cfset temp = temp[1]>
REMatch will return an array. Studying the Dom of the Google page, there is only one h2 node with a class and a defined style (let’s hope it stays that way!!!).That will return something like this :
1 Euro = 112.815605 Japanese yen
If you want to use that like it is, you are done. If you just need the value, add that:
<cfset temp = listLast(temp,'=')>
This will select the part after the equal sign.
112.815605 Japanese yen
And the following expression will extract the part in front of the first space character.
<cfset temp = listFirst(temp,' ')>
And then you have it. Now you can format that number as you want.
112.815605
Hope that is useful to someone! Here is a cfc with that function.
Note : This function is a hack and surely not ideal. The Google.com page changed and there is a pound sign now in the url which makes cfhttp request unstable. That is why I use google.co.jp for the moment. It will work if Google does not change the Dom of their result page…
By newbies for newbies! Sharing some thoughts about RIA techniques and more!
No comments.