Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 21 additions & 9 deletions classes/convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

}

Expand All @@ -107,15 +120,15 @@ 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())) {

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}");
curl_setopt($ch, CURLOPT_URL, $url);

$response = json_decode(curl_exec($ch), true);
}
Expand All @@ -124,10 +137,9 @@ protected function fetch($amount, $from, $to)
}

# Caches the rate for future
$this->newCache($from.$to, $response['rate']);
$this->newCache($from.$to, $response['amount']);

return $response;

return $response;
}

/*
Expand Down