PHP – Google Currency Converter

May 13th, 2012 by Laeeq | 1 comment

As you may know, Google has a calculator feature built in their search engine, which can be used to convert measures, currencies, get stock quotes, weather data and many other features

Here is a simple php function that converts a currency into another using the Google calculator API .
This example uses cURL , so you have to have it enabled for this code to work .

Example Code :
  1. <?php
  2. function currency($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. $rawdata = curl_exec($ch);
  14. curl_close($ch);
  15. $data = explode(‘”’, $rawdata);
  16. $data = explode(‘ ‘, $data[‘3′]);
  17. $var = $data[‘0′];
  18. return round($var,3);
  19. }
  20. ?>

Hope it will be useful for you!

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. May 14th, 2012 at 13:27 | #1

      Thanks for the good writeup. It in truth used to be a enjoyment account it. Look advanced to more introduced agreeable from you! However, how can we be in contact?

    1. No trackbacks yet.