Using the Google currency converter

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.

sConvertorUrl, $this->fPrice, $this->sFrom, $this->sTo);

		$sResponse = file_get_contents($sUrl);
		if(!$sResponse) {
			throw new Exception('Google currency convertor is not available at the moment');
		}

		$oResult = json_decode($sResponse);
		if($oResult->error != '') {
			throw new Exception('The following error occurred: '.$oResult->error);
		}

		// Isolate the price
		return (float)$oResult->rhs;
	}

	/**
	 * Get, retrieve the foreign currency
	 *
	 * @param float $fPrice
	 * @param string $sFrom, currency code (EUR, USD,...)
	 * @param string $sTo, currency code (EUR, USD,...)
	 */
	public function get($fPrice, $sFrom, $sTo)
	{
		if(!is_float($fPrice)) {
			throw new Exception('The price to convert should be a float, "'.gettype($fPrice). '" given while calling: '.__FUNCTION__);
		}

		$this->fPrice	= $fPrice;
		$this->sFrom	= strtoupper($sFrom);
		$this->sTo		= strtoupper($sTo);

		return $this->exec();
	}

}
?>

Pretty straightforward as you see.

Comments

843 responses to “Using the Google currency converter”

  1. https://mxtube.mimeld.com Avatar

    References:

    Seminole hard rock casino tampa

    References:
    https://git.esen.gay/fredricqyd304

  2. https://daterondetjolie.fr/ Avatar

    References:

    777 casino drive cherokee nc 28719

    References:
    https://www.oddmate.com/@karissa0446898

  3. git.gotrobotics.org Avatar

    References:

    Hollywood casino florida

    References:
    https://gitea.pnkx.top:8/margaritaferre

  4. codes.tools.asitavsen.com Avatar

    References:

    Hard rock casino hollywood florida

    References:
    https://gt.clarifylife.net/alexruckman47

  5. https://chinami.com Avatar

    References:

    Slot games for ipad

    References:
    https://camtalking.com/@valentinashley

  6. www.loginscotia.com Avatar

    References:

    Poker online for real money

    References:
    https://www.kingspalace.net/chelsey3445669

  7. nelgit.nelpi.co.uk Avatar

    References:

    Hardrock casino las vegas

    References:
    https://git.clubeye.net/jedvansickle71

  8. git.warze.org Avatar

    References:

    Moncton new brunswick

    References:
    https://gitea.jobiglo.com/ashli61f181649

  9. code.dsconce.space Avatar

    References:

    Montreal metro pass

    References:
    https://gitea.wuyuank.com/frederickalutw

  10. watchnpray.life Avatar

    References:

    Online casino test

    References:
    https://gitea.shizuka.icu/mickivanotterl

  11. aprileaton.net Avatar

    References:

    Egyptian treasures

    References:
    https://www.godhip.com/quentincuningh

  12. wedioz.com Avatar

    References:

    San diego casino

    References:
    https://mp3diary.com/brookchirnside

  13. vila.go.ro Avatar

    References:

    Politia de frontiera

    References:
    https://shamrick.us/maryellennall9

  14. 21pac.com Avatar

    References:

    No deposit bonus codes

    References:
    https://git.vhdltool.com/jessikax51460

Leave a Reply

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