From cd30b67c6db0dc67ef6542669e23703113631618 Mon Sep 17 00:00:00 2001 From: Ilya Medvedev Date: Wed, 29 Apr 2015 08:51:18 +0600 Subject: [PATCH 1/3] jsonrates.com API This version of script uses the jsonrates.com API, because Google's API is out of date. --- classes/convert.php | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/classes/convert.php b/classes/convert.php index eaff428..3dea0e0 100644 --- a/classes/convert.php +++ b/classes/convert.php @@ -17,6 +17,11 @@ */ Class Convert { + + /* + * This version of script uses the jsonrates.com API, because Google's API is out of date. + */ + private $jsonRatesAPI = "xxxxxxxxxxxxxxxxxxxxxx"; /* * Constructor sets to TRUE if $cacheFolder is writable @@ -98,7 +103,15 @@ public function convert($amount, $from, $to, $round = TRUE) } - return ($round) ? abs(round($return, 2)) : abs($return); + $result = ($round) ? abs(round($return, 2)) : abs($return); + + # After first script run result is equals zero + # check it and get new result + if($result == 0) { + $result = $this->convert($amount, $from, $to, $round); + } + + return $result; } @@ -107,27 +120,17 @@ public function convert($amount, $from, $to, $round = TRUE) */ protected function fetch($amount, $from, $to) - { - $url = "http://rate-exchange.appspot.com/currency?q={$amount}&from={$from}&to={$to}"; + { + $apiKey = $this->jsonRatesAPI; $amount = (float) $amount; + $url = "http://jsonrates.com/get/?amount={$amount}&from={$from}&to={$to}&apiKey={$apiKey}"; - if (in_array('curl', get_loaded_extensions())) { - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_URL, "http://rate-exchange.appspot.com/currency?q={$amount}&from={$from}&to={$to}"); - - $response = json_decode(curl_exec($ch), true); - } - else { - $response = json_decode(file_get_contents($url), true); - } + $response = json_decode(file_get_contents($url), true); # Caches the rate for future - $this->newCache($from.$to, $response['rate']); + $this->newCache($from.$to, $response['amount']); - return $response; - + return $response; } /* From f6a4eaaca8eb5c62a8d98236058e625e663ba0ab Mon Sep 17 00:00:00 2001 From: Ilya Medvedev Date: Wed, 29 Apr 2015 08:55:20 +0600 Subject: [PATCH 2/3] Curl update --- classes/convert.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/classes/convert.php b/classes/convert.php index 3dea0e0..4cdc6a8 100644 --- a/classes/convert.php +++ b/classes/convert.php @@ -125,7 +125,16 @@ protected function fetch($amount, $from, $to) $amount = (float) $amount; $url = "http://jsonrates.com/get/?amount={$amount}&from={$from}&to={$to}&apiKey={$apiKey}"; - $response = json_decode(file_get_contents($url), true); + if (in_array('curl', get_loaded_extensions())) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_URL, $url); + + $response = json_decode(curl_exec($ch), true); + } + else { + $response = json_decode(file_get_contents($url), true); + } # Caches the rate for future $this->newCache($from.$to, $response['amount']); From cab87570d2f780bed5e283375ed418b7fd4e1e92 Mon Sep 17 00:00:00 2001 From: Ilya Medvedev Date: Wed, 29 Apr 2015 07:57:49 +0500 Subject: [PATCH 3/3] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 24bf77e..d2a0b77 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ The class supports caching. The files will be cached daily. ## Usage +__Set your API Key__, which can be taken from the http://jsonrates.com for free. + Call the class. There are three optional parameters: __$cache__ - Set this to FALSE if you do not want to enabled cachine (TRUE by default)