Consectatio Excellentiae

Whilst working on a project I encountered a client conundrum of converting currencies LIVE! Where there’s a will there’s a way, so the first task was to find a resource that would convert the currency when triggered. After a few minutes searching I discovered that Yahoo finance allow you to query their service and exchange one currency to another, as long as you provide the 3 character currency code i.e. GBP or USD, British Pound and US Dollar respectively.

The next phase of the task was to establish which PHP method I would use to retrieve the data and achieve the desired conversion. I decided to use cURL because this is commonly supported on most PHP/Linux hosting accounts, and is one of PHP features I love the most (as sad as it sounds). I know there are two other techniques of retrieving data, file_get_contents() if allow_url_fopen is enabled or using a socket connection, from personal experience these two methods are more frequently disabled. However sometimes you can discover that cURL may not enabled/installed, the easiest way to find out is to copy and paste the code below and run the script and you will be provided with the answer.

if(function_exists('curl_init')){
echo 'cURL is enabled';
}else{
echo 'cURL is not enabled';
}

To proceed with this example you will need to have cURL enabled (don’t worry if its not you can still achieve this just not using this method). I have listed the function in its entirety as there is not a lot that is greatly complex about the function, most of the code relates to the cURL session which can be found in the PHP manual. Provided you enter the correct currency codes the function should return the correct result.

function convertCurrency($from,$to){

//set POST variables
 $url = 'http://download.finance.yahoo.com/d/quotes.csv';
 $fields = array(
 'e'=>urlencode('.csv'),
 'f'=>urlencode('sl1d1t1'),
 's'=>urlencode($from . $to .'=X')
 );

//url-ify the data for the POST
 foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
 rtrim($fields_string,'&');

//open connection
 $ch = curl_init();

//set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_POST,count($fields));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

//execute post
 $result = curl_exec($ch);

//close connection
 curl_close($ch);

$allData = explode(',',$result); /* Get all the contents to an array */
 return $allData[1];
}

To call the function and feed through the currency codes see the sample code below. Change the currency codes and see if the results are as you expected.

$from = 'USD';
$to = 'GBP';

$exchangedRate = convertCurrency($from,$to);
echo '1 ' . $from.' exchanged to ' . $exchangedRate . ' ' . $to;

If you would like to calculate the conversion of a rate of a set figure, you will need to slightly amend the code above. To pass a value of conversion into the function and simply multiply the $allData[1] variable by the conversion amount and boom you have your result! But I will leave that for you to toy with, if you need any assistance feel free to post a comment.

Leave a Reply