tags:

views:

207

answers:

7

I'm looking for a way to convert any amount from one currency to another on a website. The user would enter something like '100' and select USD as the currency, and then chooses Australian or Canadian dollars as the currency to convert to. When he clicks the 'Convert' button, I'd like to convert that amount automatically, through some API, and show him the amount in the currency he chose to convert to.

Any ideas?

+3  A: 

You can find a decent API at http://xurrency.com/api a call in the format below should respond with the desired output.

http://xurrency.com/api/{base}/{target}/{amount}
simnom
not enough currencies supported
Click Upvote
+1 good to know this service exists
John Conde
A: 

This isn't a trivial challenge, because currency fluctuates. Try something like this: http://www.kksou.com/php-gtk2/Joomla-Gadgets/Google-Currency-Converter-AJAX-version.php

bpeterson76
+1  A: 

A quick google search yielded:

http://blog.motane.lu/2008/12/19/currency-conversion-in-php/

which provides a php function that makes use of a google search (which allows one to convert currency) and then strips the tags.

sleepynate
It works, however google returns spaces in the result, so if it returns '74 516.3499' then the function only shows '74' as its return value. If the regexp can be fixed i'll accept the answer, here's the regexp it uses: `preg_match( '/= (([0-9]|\.|,|\ )*)/', $askGoogle, $matches );`
Click Upvote
+1  A: 

You can get the daily EURO-based exchange rates from http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

Stefan Gehrig
+1 And there is also a [PEAR package](http://pear.php.net/package/Services_ExchangeRates) for reading them (not that it would be difficult though)
Gordon
+5  A: 

From PHP Snippets

function currency($from_Currency,$to_Currency,$amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var = $data['0'];
    return round($var,2);
}

You dont necessarily need cURL for that though. file_get_contents should do too if the allow_furl_open is enabled:

$result = file_get_contents('http://www.google.com/ig/calculator?hl=en&q=100EUR=?USD');

This would return something like

{lhs: "100 Euros",rhs: "129.18 U.S. dollars",error: "",icc: true}
Gordon
And you don't need to use `explode` either, the output is in JSON which can be decoded using `json_decode()`
Click Upvote
@ClickUpvote yeah, I agree the snippet is rather odd.
Gordon
Actually I was wrong, the json_decode doesn't work, so you do have to parse it out the way that function is doing.
Click Upvote
A: 

Hi, I like the first solution but there seems to be on BIG problem with it. I tried to implement it into a payment gateway but I kept on getting a result of 1. This perplexed me and I eventually found that it is because there is a space used by google for the thousands sparator. Thus the amount of 1 500.00 was returned as one since the rest was exploded. I have created a quick and dirty fix for it. Let me know if anyone else has experienced this problem. Here is my solution:

function currency($from_Currency,$to_Currency,$amount) {
  $amount = urlencode($amount);
  $from_Currency = urlencode($from_Currency);
  $to_Currency = urlencode($to_Currency);
  $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
  $ch = curl_init();
  $timeout = 0;
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $rawdata = curl_exec($ch);
  curl_close($ch);
  $data = explode('"', $rawdata);
  $data = explode('.', $data['3']);
  $data[0] = str_replace(" ", "",preg_replace('/\D/', '',  $data[0]));
  if(isset($data[1])){
    $data[1] = str_replace(" ", "",preg_replace('/\D/', '', $data[1]));
    $var = $data[0].".".$data[1];        
  } else{
    $var = $data[0];
  }
  return round($var,2); }
A: 

Hi user481996, as the spaces are creating a problem in the explode function, why cant you trim the amount prior to exploding it ?

dskanth