承接 php-core/exchange-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-core/exchange-sdk

Composer 安装命令:

composer require php-core/exchange-sdk

包简介

A PHP SDK for the currency exchange rates API with fallback support

README 文档

README

A PHP SDK for the Currency Exchange API. This package provides an easy-to-use interface to fetch current and historical exchange rates with built-in fallback support.

Features

  • 🚀 Easy to use static methods
  • 💪 Built-in fallback support between JSDelivr and Cloudflare endpoints
  • 📅 Support for historical exchange rates
  • 🔄 Automatic retry with minified and regular JSON formats
  • ⚡ Fast and reliable with multiple CDN options
  • 🛠 Configurable endpoint preferences
  • 🔍 Comprehensive error handling
  • 💾 PSR-16 compatible caching support
  • ⚙️ Customizable HTTP client configuration

Installation

Install the package via Composer:

composer require php-core/exchange-sdk

Quick Start

use PHPCore\ExchangeSDK\Exchange;

// Get latest USD rates
$usdRates = Exchange::getLatestRates('usd');

// Get EUR/USD rate
$eurUsdRate = Exchange::getLatestRate('eur', 'usd');

// Get historical rates
$historicalRates = Exchange::getHistoricalRates('2024-03-06', 'usd');

Usage Examples

Getting Latest Exchange Rates

use PHPCore\ExchangeSDK\Exchange;

// Get all rates for USD
$usdRates = Exchange::getLatestRates('usd');
// Returns: ['date' => '2024-03-06', 'usd' => ['eur' => 0.85, 'gbp' => 0.73, ...]]

// Get specific currency pair rate
$eurUsdRate = Exchange::getLatestRate('eur', 'usd');
// Returns: 1.18 (float)

Getting Historical Rates

// Get historical rates for a specific date
$historicalRates = Exchange::getHistoricalRates('2024-03-06', 'usd');
// Returns: ['date' => '2024-03-06', 'usd' => ['eur' => 0.84, ...]]

// Get historical rate for a specific currency pair
$historicalEurUsd = Exchange::getHistoricalRate('2024-03-06', 'eur', 'usd');
// Returns: 1.08854773 (float)

Configuring Endpoints

// Set Cloudflare as the preferred endpoint globally
Exchange::setPreferredEndpoint(Exchange::ENDPOINT_CLOUDFLARE);

// Or use a specific endpoint for a single call
$rates = Exchange::getLatestRates('usd', Exchange::ENDPOINT_JSDELIVR);

Caching Support

The SDK supports PSR-16 Simple Cache for storing exchange rates. By default, it uses Symfony's FilesystemAdapter.

Default Cache Configuration

// Initialize with default filesystem cache (24-hour TTL)
Exchange::initCache();

// Rates will be automatically cached
$rates = Exchange::getLatestRates('usd');

Custom Cache Configuration

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;

// Create a custom filesystem cache
$filesystemAdapter = new FilesystemAdapter(
    'exchange-rates',    // namespace
    3600,               // TTL (1 hour)
    '/path/to/cache'    // custom directory
);
$cache = new Psr16Cache($filesystemAdapter);

// Initialize SDK with custom cache
Exchange::initCache($cache);

Using Redis Cache

use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Psr16Cache;

$redis = RedisAdapter::createConnection('redis://localhost');
$cache = new Psr16Cache(new RedisAdapter($redis));

Exchange::initCache($cache, 3600); // Cache for 1 hour

Cache Management

// Clear the cache
Exchange::clearCache();

Custom HTTP Client Configuration

use GuzzleHttp\Client;

// Configure custom Guzzle client
$client = new Client([
    'timeout' => 15,
    'connect_timeout' => 10,
    'headers' => [
        'User-Agent' => 'MyApp/1.0'
    ]
]);

Exchange::setClient($client);

Available Methods

Main Methods

  • getLatestRates(string $baseCurrency, ?string $endpoint = null): ?array
  • getHistoricalRates(string $date, string $baseCurrency, ?string $endpoint = null): ?array
  • getLatestRate(string $fromCurrency, string $toCurrency, ?string $endpoint = null): ?float
  • getHistoricalRate(string $date, string $fromCurrency, string $toCurrency, ?string $endpoint = null): ?float

Configuration Methods

  • setPreferredEndpoint(string $endpoint): void
  • initCache(?CacheInterface $cache = null, int $ttl = 86400): void
  • setClient(Client $client): void
  • clearCache(): void
  • clearClient(): void

Endpoint Constants

  • Exchange::ENDPOINT_JSDELIVR - Use JSDelivr CDN (default)
  • Exchange::ENDPOINT_CLOUDFLARE - Use Cloudflare Pages

Error Handling

The SDK handles various error scenarios gracefully:

// All methods return null on failure
$rates = Exchange::getLatestRates('invalid_currency');
if ($rates === null) {
    // Handle error
}

// Invalid endpoint configuration throws Exception
try {
    Exchange::setPreferredEndpoint('invalid_endpoint');
} catch (Exception $e) {
    // Handle error
}

Fallback Mechanism

The SDK implements a robust fallback mechanism:

  1. Try preferred endpoint
  2. If preferred endpoint fails, try fallback endpoint
  3. Results are cached for subsequent requests
  4. Cache duration is configurable

Testing

Run the test suite with PHPUnit:

./vendor/bin/phpunit

Requirements

  • PHP 7.4 or higher
  • Guzzle HTTP Client 7.0 or higher
  • PSR-16 Simple Cache implementation

License

MIT License. See LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Credits

This package is a wrapper for the Currency Exchange API by Fawaz Ahmed.

php-core/exchange-sdk 适用场景与选型建议

php-core/exchange-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 916 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 php-core/exchange-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-core/exchange-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-core/exchange-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 916
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-29