Created
June 15, 2022 12:18
-
-
Save takshaktiwari/7e1cceac466cbb98758376b5a8bc9bf9 to your computer and use it in GitHub Desktop.
Currency / Exchange rates api
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 | |
namespace App\APIs; | |
use Illuminate\Support\Facades\Http; | |
/** | |
* This API has predictable resource-oriented URLs, accepts form-encoded | |
* request bodies, returns JSON-encoded responses, and uses standard HTTP | |
* response codes, authentication, and verbs. | |
* | |
* Example usage: | |
* $currencies = (new ExchangeRate)->currencies(); | |
* | |
* @package \App\APIs\ExchangeRate | |
* @author Takshak Tiwari <takshaktiwari@gmail.com> | |
* @access public | |
* @see https://apilayer.com/marketplace/exchangerates_data-api | |
* @see https://exchangeratesapi.io/ | |
* | |
* @property string $apiKey | |
* | |
* @method array|mixed latestRates(string $base, string $symbols) | |
* @method array|mixed currencies() | |
* @method array|mixed convert(int|float $amount=1, string $from='USD', string $to='INR', ?string $date=null) | |
*/ | |
class ExchangeRate | |
{ | |
public $apiKey = 'AhKWUYzt9yX56mBnd6ZAQKFts0jgUzrS'; | |
/** | |
* Returns real-time exchange rate data updated every 60 minutes, every | |
* 10 minutes or every 60 seconds. Get latest rates of given symbols | |
* (currencies) in term of base currency. | |
* eg. $base = 'USD', $symbols = 'INR, GBP, EUR' | |
* | |
* @param string $base | |
* @param string $symbols | |
* | |
* @return array|mixed | |
*/ | |
public function latestRates(string $base='USD', string $symbols='INR') | |
{ | |
return $this->hitApi('https://api.apilayer.com/exchangerates_data/latest', [ | |
'symbols' => $symbols, | |
'base' => $base | |
]); | |
} | |
/** | |
* Returning all available currencies. Get the list of all currencies, | |
* symbol and name | |
* | |
* @return array|mixed | |
*/ | |
public function currencies() | |
{ | |
return $this->hitApi('https://api.apilayer.com/exchangerates_data/symbols'); | |
} | |
/** | |
* Currency conversion endpoint, which can be used to convert any amount | |
* from one currency to another. In order to convert currencies, please | |
* use the API's convert endpoint, append the from and to parameters and | |
* set them to your preferred base and target currency codes. | |
* | |
* @param int|float $amount required | |
* @param string $from required | |
* @param string $to required | |
* @param string|void $date optional | |
* | |
* @return array|mixed | |
*/ | |
public function convert($amount=1, $from='USD', $to='INR', $date=null) | |
{ | |
return $this->hitApi('https://api.apilayer.com/exchangerates_data/convert', [ | |
'amount' => $amount, | |
'from' => $from, | |
'to' => $to, | |
'date' => $date | |
]); | |
} | |
/** | |
* Fluctuation endpoint you will be able to retrieve information about how | |
* currencies fluctuate on a day-to-day basis. Simply append a start_date | |
* and end_date and choose which currencies (symbols). Please note that | |
* the maximum allowed time frame is 365 days. | |
* | |
* @param string $end_date required | |
* @param string $start_date required | |
* @param string|null $base optional | |
* @param string|null $symbols optional | |
* | |
* @return array|mixed | |
*/ | |
public function fluctuation(string $end_date, string $start_date, ?string $base = null, ?string $symbols = null) | |
{ | |
return $this->hitApi('https://api.apilayer.com/exchangerates_data/fluctuation', [ | |
'end_date' => $end_date, | |
'start_date' => $start_date, | |
'base' => $base, | |
'symbols' => $symbols | |
]); | |
} | |
/** | |
* Timeseries endpoint lets you query the API for daily historical rates | |
* between two dates of your choice, with a maximum time frame of 365 days. | |
* | |
* @param string $end_date required | |
* @param string $start_date required | |
* @param string|null $base optional | |
* @param string|null $symbols optional | |
* | |
* @return array|mixed | |
*/ | |
public function timeseries(string $end_date, string $start_date, ?string $base=null, ?string $symbols=null) | |
{ | |
return $this->hitApi('https://api.apilayer.com/exchangerates_data/timeseries', [ | |
'end_date' => $end_date, | |
'start_date' => $start_date, | |
'base' => $base, | |
'symbols' => $symbols | |
]); | |
} | |
/** | |
* Historical rates are available for most currencies all the way back to | |
* the year of 1999. You can query the Fixer API for historical rates by | |
* appending a date (format YYYY-MM-DD) to the base URL. | |
* | |
* @param string $date | |
* @param string|null $base | |
* @param string|null $symbols | |
* | |
* @return array|mixed | |
*/ | |
public function history(string $date, ?string $base=null, ?string $symbols=null) | |
{ | |
return $this->hitApi('https://api.apilayer.com/exchangerates_data/'.$date, [ | |
'base' => $base, | |
'symbols' => $symbols | |
]); | |
} | |
/** | |
* Hit apiLayer api and return json|array|mixed | |
* | |
* @param string $url | |
* @param array|void $params | |
* | |
* @return array|mixed | |
*/ | |
public function hitApi($url, $params = []) | |
{ | |
return Http::withHeaders([ | |
'Content-Type' => 'text/plain', | |
'apikey' => $this->apiKey | |
]) | |
->get($url, $params) | |
->json(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment