- How do I integrate Exchanger with Laravel’s HTTP client (Guzzle) for currency exchange rates?
- Use the `php-http/guzzle7-adapter` to bridge Laravel’s Guzzle client with Exchanger’s HTTPlug interface. Register the adapter in your service container, then bind Exchanger services (e.g., Fixer) with API keys from `.env`. Example: `Exchanger::service(Fixer::class)->withKey(env('FIXER_API_KEY'))`.
- Can Exchanger handle rate limit errors from providers like Fixer or CurrencyData?
- Yes, Exchanger supports fallback logic via the `Chain` service. Configure multiple providers in order of preference, and it will automatically switch to the next if a provider hits rate limits or fails. Pair this with Laravel’s queue system for retries or exponential backoff.
- Does Exchanger work with Laravel’s caching system (Redis, database, etc.) for exchange rates?
- Absolutely. Exchanger uses PSR-16/PSR-6 caching, so it integrates seamlessly with Laravel’s cache drivers. Configure a cache adapter (e.g., Redis) via `cache/simple-cache-bridge`, then set TTLs for rates. Example: `Exchanger::service(Fixer::class)->withCache($cache->driver('redis'))->ttl(3600)`.
- What Laravel versions does Exchanger support, and are there breaking changes between major releases?
- Exchanger is PHP 8.0+ compatible and works with Laravel 9+. Check the [changelog](https://github.com/florianv/exchanger/blob/master/CHANGELOG.md) for version-specific notes. Breaking changes are rare but typically involve provider API updates (e.g., Fixer’s endpoint changes). Always test in a staging environment.
- How do I fetch historical exchange rates for compliance or auditing in Laravel?
- Use the `historical()` method on supported providers (Fixer, CurrencyData, etc.). Example: `$rates = Exchanger::service(Fixer::class)->historical('2023-01-01')->fetch();`. For persistence, store results in Laravel’s database or cache driver, then query via Eloquent or a dedicated table.
- Is Exchanger suitable for high-throughput applications like e-commerce checkouts?
- Yes, but optimize for performance. Use the `Chain` service for parallel provider requests, cache aggressively (e.g., 1-hour TTL), and offload rate fetches to Laravel Queues. Monitor latency with Laravel’s logging or Prometheus. Avoid real-time fetches during peak traffic.
- How do I mock Exchanger for unit testing in Laravel?
- Use `php-http/mock-client` to simulate HTTP responses. Bind a mock client to Exchanger’s HTTPlug stack in your test setup. Example: `$mock = new MockClient(); $stack = new ClientStack($mock); Exchanger::setClient($stack)`. Test edge cases like rate limits or provider failures explicitly.
- What are the alternatives to Exchanger for Laravel currency exchange rates?
- For simplicity, consider **Swap** (by the same author), a lightweight wrapper around Exchanger. For broader financial data, explore **MoneyPHP** (for currency math) or **Laravel Money** (for monetary values). If you need crypto support, **CoinGecko’s API** or **Binance** integrations may be better, though they lack fiat-crypto hybrid features.
- How do I handle multi-tenancy in Laravel with Exchanger (e.g., per-user currency rates)?
- Cache rates per tenant using Laravel’s cache tags or a dedicated table with `tenant_id`. Example: `$cache->tags(['tenant:123'])->put('rates', $rates, 3600)`. For historical data, scope queries by tenant in Eloquent. Avoid global caching to prevent cross-tenant data leaks.
- Are there any known issues with Exchanger in production, like cache key truncation or provider deprecations?
- Cache keys are limited to 64 chars (PSR-16 constraint), so prefix them concisely (e.g., `app:rates-`). Monitor provider deprecations (e.g., exchangeratesapi.io) via their changelogs and update Exchanger manually. Set up cron jobs to refresh cached rates nightly to mitigate stale data risks.