Sjoerd Maessen blog

PHP and webdevelopment

Using the Google currency converter

without comments

For a project I’m working, I needed to get shipping rates from the UPS API (a blog post about this will follow soon).

The problem I ran into was that the UPS API returns the shipping rates with the currency of the country where the rates for are asked, so you can’t force it to always return the rates in EUR.

One solution to convert the prices is to make a very big array with all currency rates in it. I then would maintain the array every day at 9’o clock to check if the rates weren’t changed. Right, that’s not an option.

Like you might know you can easily convert currencies by using the Google search engine. For example searching on “1 Euro to dollar” will retrieve the Euro in dollar.

Below the class I wrote to convert currencies using the Google currency converter.

  1.  
  2. <?php
  3. /**
  4.  * Convert from one currency to another
  5.  *
  6.  * @author Sjoerd Maessen
  7.  */
  8. class Currency_Convertor
  9. {
  10.         /**
  11.          * Url of the google currency convertor
  12.          * @var string
  13.          */
  14.         private $sConvertorUrl = ‘http://www.google.com/ig/calculator?hl=en&q=%s%s=?%s’;
  15.  
  16.         /**
  17.          * Price to convert
  18.          * @var float
  19.          */
  20.         private $fPrice;
  21.  
  22.         /**
  23.          * From currency code
  24.          * @var string
  25.          */
  26.         private $sFrom;
  27.  
  28.         /**
  29.          * To currency code
  30.          * @var string
  31.          */
  32.         private $sTo;
  33.  
  34.         /**
  35.          * Build up our query url and catch errors
  36.          *
  37.          * @return boolean $bResult
  38.          */
  39.         private function exec()
  40.         {
  41.                 $bResult        = false;
  42.                 $sUrl           = sprintf($this->sConvertorUrl, $this->fPrice, $this->sFrom, $this->sTo);
  43.  
  44.                 $sResponse = file_get_contents($sUrl);
  45.                 if(!$sResponse) {
  46.                         throw new Exception(‘Google currency convertor is not available at the moment’);
  47.                 }
  48.  
  49.                 $oResult = json_decode($sResponse);
  50.                 if($oResult->error != ) {
  51.                         throw new Exception(‘The following error occurred: ‘.$oResult->error);
  52.                 }
  53.  
  54.                 // Isolate the price
  55.                 return (float)$oResult->rhs;
  56.         }
  57.  
  58.         /**
  59.          * Get, retrieve the foreign currency
  60.          *
  61.          * @param float $fPrice
  62.          * @param string $sFrom, currency code (EUR, USD,…)
  63.          * @param string $sTo, currency code (EUR, USD,…)
  64.          */
  65.         public function get($fPrice, $sFrom, $sTo)
  66.         {
  67.                 if(!is_float($fPrice)) {
  68.                         throw new Exception(‘The price to convert should be a float, "’.gettype($fPrice). ‘" given while calling: ‘.__FUNCTION__);
  69.                 }
  70.  
  71.                 $this->fPrice   = $fPrice;
  72.                 $this->sFrom    = strtoupper($sFrom);
  73.                 $this->sTo              = strtoupper($sTo);
  74.  
  75.                 return $this->exec();
  76.         }
  77.  
  78. }
  79. ?>
  80.  

Pretty straightforward as you see.

Written by Sjoerd Maessen

January 11th, 2010 at 8:56 pm

Posted in API

Tagged with ,

Leave a Reply