Tim Nash
Global Moderator Community Supporter?
Internet Junkie
Posts: 2173
5036 credits Members referred : 2
Venture Skills - New Media & IT group
« on: Mar 10, 2007, 11:27:59 PM »
I had need for this a few days ago, and in the end hacked together this version, it uses CURL and calls Google to get current currencies it can be easily modified, so if people fancy making variants post them up.
so exchange.php
Code:
<?php function exchangeRate( $amount, $currency, $exchangeIn ) { $googleQuery = $amount . ' ' . $currency . ' in ' . $exchangeIn; $googleQuery = urlEncode( $googleQuery ); $ch = curl_init(); //set curl variable $timeout = 10; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://www.google.com/search?q=' . $googleQuery); //call to Google Servers curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); $file_contents = strip_tags( $file_contents ); curl_close($ch); $matches = array(); preg_match( '/= (([0-9]|\.|,|\ )*)/', $file_contents, $matches ); return $matches[1] ? $matches[1] : false; } ?>
And to call the function simply include the following in your page
its pretty straight forward, exchangeRate( 100, 'GBP', 'usd' ) In this example the 100 is the value, the GBP is the default currency and usd is the currency to convert.
Tim Nash
Global Moderator Community Supporter?
Internet Junkie
Posts: 2173
5036 credits Members referred : 2
Venture Skills - New Media & IT group
« Reply #2 on: Mar 11, 2007, 12:24:28 AM »
I will write up a more detailed tutorial tomorrow and put it in. Its a nice intro to using Google services and CURL so would make a nice programming tutorial
Global Moderator
Internet Junkie
Gender:
Posts: 1807
9006 credits Members referred : 6
« Reply #4 on: Mar 12, 2007, 12:51:29 PM »
excellent piece of code! I'm updating conversion rates manually on one of my websites every week (if I remember to do the update that is ) This code will be used instead from now on!