PHP: Currency Converter via Google Finance

Recently, I searched a way to get the conversion of some money. I usually use the function in google.

Google Currency Convert

Google Currency Convert

After some research, I found this website : google finance API. Some guys work with this websites.
This one give us a class in PHP.

I try to use it but I got an error with a regular expression. As I’m bad, I remove it and work with a string. I’m pretty sur that it is slower but it is enough efficient for my need.

/**
 * Based on
 * http://www.chazzuka.com/blog/?p=104
 * PHP class Google Currency Converter
 * Feb 06 2009
 * webmaster@chazzuka.com
 * with my modification :
 * work with string instead of regular expression
 */
	class GoogleCurrencyConverter
	{
		CONST GOOGLE_URL = "http://finance.google.com/finance/converter?a=%d&from=%s&to=%s";
		private function load($a,$from,$to)
		{
			$ch = curl_init ();
				  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				  curl_setopt($ch, CURLOPT_URL, sprintf(self::GOOGLE_URL,$a,$from,$to));
				  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
				  $response = curl_exec($ch);
				  curl_close($ch);
			return $response;
		}
		public static function convert($a,$from,$to)
		{
			//if the currency is the same return thr same amount
			if($from !='EUR')
			{
				$response = self::load($a,$from,$to);
				$domaine = strstr($response, '<span class=bld>');
				return floatval(substr($domaine,16,strpos($domaine, '&') ));
			}
			else
				return $a;
		}
	}
This entry was posted in Code Snippet, PHP and tagged . Bookmark the permalink.

2 Responses to PHP: Currency Converter via Google Finance

  1. Tetardo says:

    http://oohhyeah.blogspot.com/2009/01/google-curre...

    Here is templage in php:
    $amount = "100";
    $from_Currency = "EUR";
    $to_Currency = "AUD";

    $query_URL = "http://www.google.com/ig/calculator?hl=en&q=$amount$to_Currency.%3D%3F$to_Currency";

    And the resposnse looks like this:
    {lhs: "100 Euros",rhs: "191.745037 Australian dollars",display: "DISPLAY_FULL_PAGE",error: "",icc: true}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">