Last active
November 22, 2017 12:07
-
-
Save gabrieljmj/6977968 to your computer and use it in GitHub Desktop.
Currency converter using Google API You must have the curl extension in PHP active.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author @GabrielJMJ /twitter | |
* @description Abstract class to define a interface of a converter | |
* @link https://gist.github.com/GabrielJMJ/6977968 Gist on GitHub | |
**/ | |
abstract class Converter{ | |
protected $valueWithCurrency; | |
protected $toCurrency; | |
protected $convertedValue; | |
public function __construct( $valueWithCurrency, $toCurrency ){ | |
$this->valueWithCurrency = $valueWithCurrency; | |
$this->toCurrency = $toCurrency; | |
} | |
public function getConvertedValue(){ | |
return $this->convertedValue; | |
} | |
abstract public function convert(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
United Arab Emirates Dirham (AED) | |
Netherlands Antillean Guilder (ANG) | |
Argentine Peso (ARS) | |
Australian Dollar (AUD) | |
Bangladeshi Taka (BDT) | |
Bulgarian Lev (BGN) | |
Bahraini Dinar (BHD) | |
Brunei Dollar (BND) | |
Bolivian Boliviano (BOB) | |
Brazilian Real (BRL) | |
Botswanan Pula (BWP) | |
Canadian Dollar (CAD) | |
Swiss Franc (CHF) | |
Chilean Peso (CLP) | |
Chinese Yuan (CNY) | |
Colombian Peso (COP) | |
Costa Rican Colón (CRC) | |
Czech Republic Koruna (CZK) | |
Danish Krone (DKK) | |
Dominican Peso (DOP) | |
Algerian Dinar (DZD) | |
Estonian Kroon (EEK) | |
Egyptian Pound (EGP) | |
Euro (EUR) | |
Fijian Dollar (FJD) | |
British Pound Sterling (GBP) | |
Hong Kong Dollar (HKD) | |
Honduran Lempira (HNL) | |
Croatian Kuna (HRK) | |
Hungarian Forint (HUF) | |
Indonesian Rupiah (IDR) | |
Israeli New Sheqel (ILS) | |
Indian Rupee (INR) | |
Jamaican Dollar (JMD) | |
Jordanian Dinar (JOD) | |
Japanese Yen (JPY) | |
Kenyan Shilling (KES) | |
South Korean Won (KRW) | |
Kuwaiti Dinar (KWD) | |
Cayman Islands Dollar (KYD) | |
Kazakhstani Tenge (KZT) | |
Lebanese Pound (LBP) | |
Sri Lankan Rupee (LKR) | |
Lithuanian Litas (LTL) | |
Latvian Lats (LVL) | |
Moroccan Dirham (MAD) | |
Moldovan Leu (MDL) | |
Macedonian Denar (MKD) | |
Mauritian Rupee (MUR) | |
Maldivian Rufiyaa (MVR) | |
Mexican Peso (MXN) | |
Malaysian Ringgit (MYR) | |
Namibian Dollar (NAD) | |
Nigerian Naira (NGN) | |
Nicaraguan Córdoba (NIO) | |
Norwegian Krone (NOK) | |
Nepalese Rupee (NPR) | |
New Zealand Dollar (NZD) | |
Omani Rial (OMR) | |
Peruvian Nuevo Sol (PEN) | |
Papua New Guinean Kina (PGK) | |
Philippine Peso (PHP) | |
Pakistani Rupee (PKR) | |
Polish Zloty (PLN) | |
Paraguayan Guarani (PYG) | |
Qatari Rial (QAR) | |
Romanian Leu (RON) | |
Serbian Dinar (RSD) | |
Russian Ruble (RUB) | |
Saudi Riyal (SAR) | |
Seychellois Rupee (SCR) | |
Swedish Krona (SEK) | |
Singapore Dollar (SGD) | |
Slovak Koruna (SKK) | |
Sierra Leonean Leone (SLL) | |
Salvadoran Colón (SVC) | |
Thai Baht (THB) | |
Tunisian Dinar (TND) | |
Turkish Lira (TRY) | |
Trinidad and Tobago Dollar (TTD) | |
New Taiwan Dollar (TWD) | |
Tanzanian Shilling (TZS) | |
Ukrainian Hryvnia (UAH) | |
Ugandan Shilling (UGX) | |
US Dollar (USD) | |
Uruguayan Peso (UYU) | |
Uzbekistan Som (UZS) | |
Venezuelan Bolíar (VEF) | |
Vietnamese Dong (VND) | |
CFA Franc BCEAO (XOF) | |
Yemeni Rial (YER) | |
South African Rand (ZAR) | |
Zambian Kwacha (ZMK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$converter = new GoogleCurrencyConverter( '1USD', 'BRL' ); | |
$converter->convert(); | |
echo $converter->getConvertedValue(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author GabrielJMJ /twitter | |
* @description A class to use Google's Currency Converter | |
* @link https://gist.github.com/GabrielJMJ/6977968 Gist on GitHub | |
**/ | |
class GoogleCurrencyConverter extends Converter{ | |
private $contents; | |
private $jsonResult; | |
public function convert(){ | |
$this->getGoogleCurrencyConverterContents(); | |
$this->convertToValidJson(); | |
$this->convertedValue = $this->jsonResult->rhs; | |
} | |
private function getGoogleCurrencyConverterContents(){ | |
$url = sprintf( | |
'http://www.google.com/ig/calculator?hl=en&q=%s=?%s', | |
trim( str_replace( ' ', '', $this->valueWithCurrency ) ), | |
trim( $this->toCurrency ) | |
); | |
$ch = curl_init(); | |
$timeout = 0; | |
curl_setopt ( $ch, CURLOPT_URL, $url ); | |
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout ); | |
$this->contents = curl_exec( $ch ); | |
curl_close( $ch ); | |
} | |
private function convertToValidJson(){ | |
$str_replace = array( | |
'lhs' => '"lhs"', | |
'rhs' => '"rhs"', | |
'error' => '"error"', | |
'icc' => '"icc"' | |
); | |
$newContents = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $this->contents ); | |
$this->jsonResult = json_decode( $newContents ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment