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. gitea.jb1000.com Avatar

    References:

    Casino drive furiani

    References:
    https://hiwifi.denq.us:8418/jaymethow63893

  2. superocho.org Avatar

    References:

    Western lotto max

    References:
    https://rsas.de/shalandawaid91

  3. my explanation find here official source click over here navigate to this website see this read full report more inside more info visit site my link here this website their explanation her comment is here discover more Avatar

    Hey! This post couldn’t be written any better! Reading this post reminds me of my good old room mate! He always kept chatting about this. I will forward this write-up to him. Pretty sure he will have a good read. Thanks for sharing!

  4. git.miasma-os.com Avatar

    References:

    Casino sanremo online

    References:
    https://git.ihatemen.uk/georgiasquires

  5. wiibiplay.fun Avatar

    References:

    Tucson casino

    References:
    https://gitea.hadi.es/kaylapetro3300

  6. https://www.livecima.com/ Avatar

    References:

    Lady luck casino vicksburg ms

    References:
    https://gitea.scivigi.com/johnathanferri

  7. https://gitea.adber.tech/ Avatar

    References:

    Casino aztar evansville

    References:
    https://qflirt.net/@danielariordan

  8. git.ultra.pub Avatar

    References:

    Online slots for real money

    References:
    https://gitea.nongnghiepso.com/kobycrutcher8

  9. ns2.asso-web.com Avatar

    References:

    Hunger games online game

    References:
    https://datingmywish.com/@beulahspoffort

  10. git.cymnb.com Avatar

    References:

    George thorogood i drink alone

    References:
    https://drarchina.com/demo/@alysaring44052?page=about

  11. Golf 8R vierwielaandrijving Avatar

    Respect to post author, some good information .

  12. Binance账户创建 Avatar

    Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.binance.info/register?ref=JW3W4Y3A

Leave a Reply

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