Skip to content

Commit

Permalink
Merge pull request #6 from imanilchaudhari/development
Browse files Browse the repository at this point in the history
fixer, currencylayer and json rates api added
  • Loading branch information
imanilchaudhari authored Apr 8, 2018
2 parents 4b632e2 + 5fd11d6 commit fccfd60
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Provider/CurrencylayerApi.php
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)];
}
}
38 changes: 38 additions & 0 deletions Provider/FixerApi.php
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)];
}
}
38 changes: 38 additions & 0 deletions Provider/JsonRatesApi.php
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)];
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Yii2 Currency Converter
=======================
[![Build Status](https://travis-ci.org/imanilchaudhari/yii2-currency-converter.svg?branch=master)](https://travis-ci.org/imanilchaudhari/yii2-currency-converter)
[![Latest Stable Version](https://poser.pugx.org/imanilchaudhari/yii2-currency-converter/version)](https://packagist.org/packages/imanilchaudhari/yii2-currency-converter)
[![Latest Unstable Version](https://poser.pugx.org/imanilchaudhari/yii2-currency-converter/v/unstable)](//packagist.org/packages/imanilchaudhari/yii2-currency-converter)
[![Total Downloads](https://poser.pugx.org/imanilchaudhari/yii2-currency-converter/downloads)](https://packagist.org/packages/imanilchaudhari/yii2-currency-converter)

This extension will help to find out current currency conversion rate. This extension uses Yahoo's currency conversion API.

Why Use It
Expand Down
22 changes: 22 additions & 0 deletions docs/FixerApi.md
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;
}
}
```
25 changes: 25 additions & 0 deletions docs/OpenExchangeRatesApi.md
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;
}
}
```
25 changes: 25 additions & 0 deletions docs/README.md
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;
}
}
```
25 changes: 25 additions & 0 deletions docs/YahooApi.md
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;
}
}
```

0 comments on commit fccfd60

Please sign in to comment.