forexapi/client
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.
forexapi/client 适用场景与选型建议
forexapi/client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「client」 「currency」 「converter」 「finance」 「rates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 forexapi/client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 forexapi/client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 forexapi/client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Fixer.io data provider for Peso
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
TCMB Currenct Converter
Immutable PHP currency converter that's data-agnostic.
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-09
