Convert Currency Using Google API

August 16th, 2012 by Laeeq | 2 comments

google currency converter

As most of us uses Google for searching web information. But very few people know that Google also provides Currency Converter apps for converting currency to our desired currency. We can see the current status of our currency into other currency by going through Google’s currency converter page. However as a Web developer, sometimes we need to implement Google currency converter functionality on our application. This is very simple; you can implement it very easily using PHP script. it just require CURL extension to be installed on your server to make request to Google currency convert API and can get your desired result.

Below is a complete and test PHP currency converter script using Google API which have a function currencyConverter($from_Currency,$to_Currency,$amount) with three parameters. The first parameter is $from_Currency, the second is $to_Currency means the currency in which it is converted. And third parameter is $amount that will be passed for conversion.

PHP currency convert  functions:

  1. <?php
  2. function currencyConverter($from_Currency,$to_Currency,$amount) {
  3. $amount = urlencode($amount);
  4. $from_Currency = urlencode($from_Currency);
  5. $to_Currency = urlencode($to_Currency);
  6. $url = “http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency”;
  7. $ch = curl_init();
  8. $timeout = 0;
  9. curl_setopt ($ch, CURLOPT_URL, $url);
  10. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($ch,  CURLOPT_USERAGENT , “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”);
  12. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  13. $myrawdata = curl_exec($ch);
  14. curl_close($ch);
  15. $mydata = explode(‘”‘, $myrawdata);
  16. $mydata = explode(‘ ’, $mydata['3']);
  17. $mycurrency = $mydata['0'];
  18. $converted_currency=”Currency Conversion:
  19. Date: “.date(“m/d/Y h:i:s“).”
  20. From $from_Currency($amount) To $to_Currency: ”.round($mycurrency,3);
  21. return $converted_currency;
  22. }
  23. ?>

PHP currency convert  function call:

  1. <?php
  2. // change amount according to your needs
  3. $amount =10;
  4. // change From Currency according to your needs
  5. $from_Curr =“INR”;
  6. // change To Currency according to your needs
  7. $to_Curr =“USD”;
  8. $converted_currency=currencyConverter($from_Curr, $to_Curr, $amount);
  9. // Print outout
  10. echo $converted_currency;
  11. ?>

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. August 16th, 2012 at 23:18 | #1

    Hey dude. what kind of wordpress theme are you using? i want it to use on my blog too .

  2. August 17th, 2012 at 01:50 | #2

    Thanks for giving your ideas in this article.

  1. No trackbacks yet.