ceytek-labs / tcmb
灵活且可扩展的TCMB PHP包;提供访问汇率和其他TCMB API数据的功能。
v1.1.3
2024-09-22 16:10 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.9
This package is not auto-updated.
Last update: 2024-09-22 22:34:48 UTC
README
TCMB PHP包
TCMB(土耳其共和国中央银行)PHP包旨在帮助您轻松利用TCMB提供的各种数据和API。目前,它提供获取汇率的功能,但未来可以扩展以包括TCMB的其他API。
要求
- PHP 8.1或更高版本
功能
- 汇率:从TCMB获取当前汇率。
- 灵活结构:您可以选择以对象或数组格式接收数据。
- 可扩展性:构建了一个适合未来集成其他TCMB API的基础设施。
安装
您可以使用Composer将此包添加到项目中
composer require ceytek-labs/tcmb
用法
以下是基本用法示例和包的说明。
显示API的原始XML结果
use CeytekLabs\Tcmb\ExchangeRates; // Displays the raw XML result from the API echo ExchangeRates::make()->response();
显示API的原始JSON结果
use CeytekLabs\Tcmb\ExchangeRates; // Converts the raw XML result from the API to JSON and displays it echo ExchangeRates::make()->jsonContent();
格式化汇率
您可以从API以对象或数组格式接收数据。对象格式使用驼峰命名法,数组格式使用蛇形命名法。
use CeytekLabs\Tcmb\ExchangeRates; use CeytekLabs\Tcmb\Enums\Format; // Getting data in object format $exchangeRatesObject = ExchangeRates::make()->format(Format::Object)->content(); // Getting data in array format $exchangeRatesArray = ExchangeRates::make()->format(Format::Array)->content();
获取所有汇率
use CeytekLabs\Tcmb\ExchangeRates; use CeytekLabs\Tcmb\Enums\Format; // Getting all exchange rates in object format $currenciesObject = ExchangeRates::make()->format(Format::Object)->currencies(); // Getting all exchange rates in array format $currenciesArray = ExchangeRates::make()->format(Format::Array)->currencies();
获取特定货币
您可以使用currency()
方法获取特定货币的数据。
use CeytekLabs\Tcmb\ExchangeRates; use CeytekLabs\Tcmb\Enums\Format; use CeytekLabs\Tcmb\Enums\Currency; // For example, getting the Australian Dollar (AUD) rate $australianDollar = ExchangeRates::make() ->format(Format::Object) ->currency(Currency::AustralianDollar) ->first();
访问货币信息
您可以访问特定货币的详细信息
use CeytekLabs\Tcmb\ExchangeRates; use CeytekLabs\Tcmb\Enums\Currency; // Getting information for the USD currency $exchangeRates = ExchangeRates::make() ->format(Format::Object) ->currency(Currency::UnitedStatesDollar); // Currency code echo $exchangeRates->code(); // "USD" // Turkish name echo $exchangeRates->turkishName(); // "ABD DOLARI" // English name echo $exchangeRates->englishName(); // "US DOLLAR" // Unit amount echo $exchangeRates->unit(); // "1" // Forex buying rate (The buying rate valid in international markets and digital transactions) echo $exchangeRates->forexBuying(); // Forex selling rate (The selling rate valid in international markets and digital transactions) echo $exchangeRates->forexSelling(); // Banknote buying rate (The buying rate valid in cash currency transactions) echo $exchangeRates->banknoteBuying(); // Banknote selling rate (The selling rate valid in cash currency transactions) echo $exchangeRates->banknoteSelling();
示例输出
以对象格式显示所有数据
use CeytekLabs\Tcmb\ExchangeRates; use CeytekLabs\Tcmb\Enums\Format; print_r(ExchangeRates::make()->format(Format::Object)->content());
示例输出
stdClass Object ( [attributes] => stdClass Object ( [date] => "2024-09-20 15:30" [bulletinNumber] => "2024/178" ) [currencies] => stdClass Object ( [USD] => stdClass Object ( [code] => "USD" [name] => stdClass Object ( [tr] => "ABD DOLARI" [en] => "US DOLLAR" ) [unit] => "1" [forex] => stdClass Object ( [buying] => "33.9531" [selling] => "34.0142" ) [banknote] => stdClass Object ( [buying] => "33.9293" [selling] => "34.0652" ) ) // Other currencies... ) )
以数组格式显示所有数据
use CeytekLabs\Tcmb\ExchangeRates; use CeytekLabs\Tcmb\Enums\Format; print_r(ExchangeRates::make()->format(Format::Array)->content());
示例输出
Array ( [attributes] => Array ( [date] => "2024-09-20 15:30" [bulletin_number] => "2024/178" ) [currencies] => Array ( [USD] => Array ( [code] => "USD" [name] => Array ( [tr] => "ABD DOLARI" [en] => "US DOLLAR" ) [unit] => "1" [forex] => Array ( [buying] => "33.9531" [selling] => "34.0142" ) [banknote] => Array ( [buying] => "33.9293" [selling] => "34.0652" ) ) // Other currencies... ) )
支持货币
该包支持以下货币
- 🇺🇸 USD:美元
- 🇦🇺 AUD:澳大利亚元
- 🇩🇰 DKK:丹麦克朗
- 🇪🇺 EUR:欧元
- 🇬🇧 GBP:英镑
- 🇨🇭 CHF:瑞士法郎
- 🇸🇪 SEK:瑞典克朗
- 🇨🇦 CAD:加拿大元
- 🇰🇼 KWD:科威特第纳尔
- 🇳🇴 NOK:挪威克朗
- 🇸🇦 SAR:沙特里亚尔
- 🇯🇵 JPY:日元
- 🇧🇬 BGN:保加利亚列弗
- 🇷🇴 RON:罗马尼亚列伊
- 🇷🇺 RUB:俄罗斯卢布
- 🇮🇷 IRR:伊朗里亚尔
- 🇨🇳 CNY:人民币
- 🇵🇰 PKR:巴基斯坦卢比
- 🇶🇦 QAR:卡塔尔里亚尔
- 🇰🇷 KRW:韩元
- 🇦🇿 AZN:阿塞拜疆马纳特
- 🇦🇪 AED:阿联酋迪拉姆
调试
- 如果API响应无效或发生错误,将抛出
Exception
。 - 如果XML格式无效,您将收到以下错误:
Invalid XML format. Please check ExchangeRates::make()->getResponse()
未来计划
此包可以扩展以包括TCMB提供的其他数据和服务的,例如
- 利率:获取TCMB发布的利率。
- 通货膨胀数据:访问当前通货膨胀数据。
- 其他金融数据:集成TCMB提供的其他统计和报告。
贡献
如果您想贡献,请发送pull request或报告问题。
许可证
该项目采用MIT许可证。