-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from imanilchaudhari/development
fixer, currencylayer and json rates api added
- Loading branch information
Showing
8 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace imanilchaudhari\CurrencyConverter\Provider; | ||
|
||
use yii\base\Component; | ||
|
||
class CurrencylayerApi extends Component implements ProviderInterface | ||
{ | ||
/** | ||
* Url where Curl request is made | ||
* | ||
* @var string | ||
*/ | ||
const API_URL = 'http://www.apilayer.net/api/live?access_key=49af376f2fc6c1312fa58d695df5a026&source=USD&curriences=GBP&format=1'; | ||
|
||
/** | ||
* The Api Layer access_key | ||
* | ||
* @var string | ||
*/ | ||
public $access_key; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRate($fromCurrency, $toCurrency) | ||
{ | ||
$fromCurrency = urlencode($fromCurrency); | ||
|
||
$url = str_replace(['[fromCurrency]'], [$fromCurrency], static::API_URL); | ||
|
||
$ch = curl_init(); | ||
$timeout = 0; | ||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)'); | ||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | ||
$rawdata = curl_exec($ch); | ||
curl_close($ch); | ||
|
||
$parsedData = json_decode($rawdata, true); | ||
|
||
return $parsedData['rates'][strtoupper($toCurrency)]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace imanilchaudhari\CurrencyConverter\Provider; | ||
|
||
use yii\base\Component; | ||
|
||
class FixerApi extends Component implements ProviderInterface | ||
{ | ||
/** | ||
* Url where Curl request is made | ||
* | ||
* @var string | ||
*/ | ||
const API_URL = 'http://api.fixer.io/latest?base=[fromCurrency]'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRate($fromCurrency, $toCurrency) | ||
{ | ||
$fromCurrency = urlencode($fromCurrency); | ||
|
||
$url = str_replace(['[fromCurrency]'], [$fromCurrency], static::API_URL); | ||
|
||
$ch = curl_init(); | ||
$timeout = 0; | ||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)'); | ||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | ||
$rawdata = curl_exec($ch); | ||
curl_close($ch); | ||
|
||
$parsedData = json_decode($rawdata, true); | ||
|
||
return $parsedData['rates'][strtoupper($toCurrency)]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace imanilchaudhari\CurrencyConverter\Provider; | ||
|
||
use yii\base\Component; | ||
|
||
class JsonRatesApi extends Component implements ProviderInterface | ||
{ | ||
/** | ||
* Url where Curl request is made | ||
* | ||
* @var string | ||
*/ | ||
const API_URL = 'http://jsonrates.com/get/?from=[fromCurrency]&to=[toCurrency]'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRate($fromCurrency, $toCurrency) | ||
{ | ||
$fromCurrency = urlencode($fromCurrency); | ||
|
||
$url = str_replace(['[fromCurrency]', '[toCurrency]'], [$fromCurrency, $toCurrency], static::API_URL); | ||
|
||
$ch = curl_init(); | ||
$timeout = 0; | ||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)'); | ||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | ||
$rawdata = curl_exec($ch); | ||
curl_close($ch); | ||
|
||
$parsedData = json_decode($rawdata, true); | ||
|
||
return $parsedData['rates'][strtoupper($toCurrency)]; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Fixer APi Integration | ||
----------------------------------- | ||
```php | ||
|
||
use Yii; | ||
use imanilchaudhari\CurrencyConverter\Provider\FixerApi; | ||
|
||
class CurrencyConverter extends \imanilchaudhari\CurrencyConverter\CurrencyConverter | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRateProvider() | ||
{ | ||
if (!$this->rateProvider) { | ||
$this->setRateProvider(new FixerApi()); | ||
} | ||
|
||
return $this->rateProvider; | ||
} | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Open Exchange Rates APi Integration | ||
----------------------------------- | ||
Here is a code snippets suggested by [chaimleich](https://github.com/chaimleich) on [this pull request](https://github.com/imanilchaudhari/yii2-currency-converter/pull/3). | ||
```php | ||
|
||
use Yii; | ||
use imanilchaudhari\CurrencyConverter\Provider\OpenExchangeRatesApi; | ||
|
||
class CurrencyConverter extends \imanilchaudhari\CurrencyConverter\CurrencyConverter | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRateProvider() | ||
{ | ||
if (!$this->rateProvider) { | ||
$this->setRateProvider(new OpenExchangeRatesApi([ | ||
'appId' => Yii::$app->params['openExchangeRate']['appId'], | ||
])); | ||
} | ||
|
||
return $this->rateProvider; | ||
} | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Open Exchange Rates APi Integration | ||
----------------------------------- | ||
Here is a code snippets suggested by [chaimleich](https://github.com/chaimleich) on [this pull request](https://github.com/imanilchaudhari/yii2-currency-converter/pull/3). | ||
```php | ||
|
||
use Yii; | ||
use imanilchaudhari\CurrencyConverter\Provider\OpenExchangeRatesApi; | ||
|
||
class CurrencyConverter extends \imanilchaudhari\CurrencyConverter\CurrencyConverter | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRateProvider() | ||
{ | ||
if (!$this->rateProvider) { | ||
$this->setRateProvider(new OpenExchangeRatesApi([ | ||
'appId' => Yii::$app->params['openExchangeRate']['appId'], | ||
])); | ||
} | ||
|
||
return $this->rateProvider; | ||
} | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Open Exchange Rates APi Integration | ||
----------------------------------- | ||
Here is a code snippets suggested by [chaimleich](https://github.com/chaimleich) on [this pull request](https://github.com/imanilchaudhari/yii2-currency-converter/pull/3). | ||
```php | ||
|
||
use Yii; | ||
use imanilchaudhari\CurrencyConverter\Provider\OpenExchangeRatesApi; | ||
|
||
class CurrencyConverter extends \imanilchaudhari\CurrencyConverter\CurrencyConverter | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getRateProvider() | ||
{ | ||
if (!$this->rateProvider) { | ||
$this->setRateProvider(new OpenExchangeRatesApi([ | ||
'appId' => Yii::$app->params['openExchangeRate']['appId'], | ||
])); | ||
} | ||
|
||
return $this->rateProvider; | ||
} | ||
} | ||
``` |