forexapi/client
最新稳定版本:1.0.1
Composer 安装命令:
composer require forexapi/client
包简介
Library for consuming the ForexAPI. Ideal for those in the financial or e-commerce sectors, this client simplifies the process of incorporating real-time forex data into various PHP-based projects.
README 文档
README
ForexAPI PHP Client
This is a PHP client for the ForexAPI. It provides an easy-to-use interface for interacting with the API's endpoints.
The ForexAPI offers a free plan and provides foreign exchange rates and currency conversion.
The API documentation can be found at https://forexapi.eu/en/docs.
Requirements
- PHP 7.4 or higher
- json extension
- Composer
Installation
Use Composer to install the ForexAPI PHP Client:
composer require forexapi/client
This package does not come with a Http Client. You can use any PSR-18 compatible client.
If you have multiple Http Clients installed, you can specify which one to use.
composer require guzzlehttp/guzzle forexapi/client
Usage
Create an instance of the Client class directly with your API key:
use ForexAPI\Client\Client; $client = new Client('your-api-key');
Create new instance using the ForexAPI\Client\ForexAPIClientBuilder class:
use \ForexAPI\Client\ForexAPIClientBuilder; $builder = (new ForexAPIClientBuilder()) ->withApiKey('your-api-key') ->withBaseUri('https://forexapi.eu/api/') ->withHttpAdapter($yourCustomHttpAdapter) ->build() ;
Using your own PSR-18 client and PSR-17 request factory is optional. If you don't provide them, the client will try to discover them automatically.
use \ForexAPI\Client\ForexAPIClientBuilder; $builder = (new ForexAPIClientBuilder()) ->withApiKey('your-api-key') ->withPsr18Client($yourCustomPsr18Client) ->withPsr17RequestFactory($yourCustomPsr17RequestFactory) ->build() ;
Get Live Quote
To get a live quote for a specific currency pair:
$quote = $client->getLiveQuote('USD', 'PLN'); echo $quote->getBase(); // USD echo $quote->getCounter(); // PLN echo $quote->getBid(); // Bid price echo $quote->getAsk(); // Ask price echo $quote->getMid(); // Mid price echo $quote->getTimestamp(); // Timestamp
Get Exchange Rate
To get the exchange rate between two currencies:
$exchangeRate = $client->getExchangeRate('USD', 'PLN'); echo $exchangeRate->getFrom(); // USD echo $exchangeRate->getTo(); // PLN echo $exchangeRate->getRate(); // Exchange rate echo $exchangeRate->getTimestamp(); // Timestamp
If you would like to get multiple exchange rates at once, you can use the getExchangeRates method.
It accepts an array of counter currencies as an argument:
$exchangeRate = $client->getExchangeRates('USD', ['PLN', 'EUR', 'GBP']);
Convert Currency
To convert an amount from one currency to another:
$conversion = $client->convert('USD', 'PLN', 100.0); echo $conversion->getFrom(); // USD echo $conversion->getTo(); // PLN echo $conversion->getAmount(); // 100.0 echo $conversion->getResult(); // Converted amount in PLN echo $conversion->getTimestamp(); // Timestamp
If you would like to convert to multiple currencies at once, you can use the convertMany method:
$conversions = $client->convertMany('USD', ['PLN', 'EUR', 'GBP'], 100.0);
Testing
This library includes a suite of unit tests. Run them using PHPUnit:
./vendor/bin/phpunit
License
This project is licensed under the MIT License. See the LICENSE file for details.
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-09
