A modern PHP 8.4+ HTTP client factory and middleware library specifically designed for marketplace API integrations. Provides unified abstractions for different HTTP transport methods, comprehensive rate limiting, authentication, and error handling.
- Strict typing with
declare(strict_types=1) - Property hooks and constructor property declarations
- PSR-4 autoloading and PSR-18 HTTP client interface compliance
- PHPStan level 8 compliance
- Amazon SP-API: Dynamic rate limiting, LWA authentication, feed uploads
- eBay API: Trading API support, OAuth 2.0, inventory management
- Discogs API: OAuth 1.0a authentication, conservative rate limiting
- Bandcamp API: Multi-seller support, stream context transport
- cURL Transport: Full-featured, HTTP/2 support, file uploads
- Stream Transport: No dependencies,
stream_context_createbased - Auto-detection: Automatically selects best available transport
- Token Bucket: Burst support (Amazon, Bandcamp)
- Fixed Window: Daily quotas (eBay)
- Sliding Window: Precise control (Discogs)
- Response Analysis: Auto-adjusts based on API headers
- Bearer tokens, API keys, Basic auth
- OAuth 2.0 with automatic token refresh
- OAuth 1.0a with signature generation (Discogs)
- Custom authentication providers
- Comprehensive middleware system
- Automatic retries with exponential backoff
- Request/response logging with PSR-3
- Circuit breaker patterns
- Performance monitoring
composer require four-bytes/four-marketplace-http-client- PHP 8.4+ with strict typing support
- Symfony HTTP Client 7.2+ for base functionality
- Symfony Rate Limiter 7.2+ for rate limiting
- PSR-3 Logger for logging support (optional)
- PSR-6 Cache for caching support (optional)
- cURL for advanced HTTP features (recommended)
- OpenSSL for SSL/TLS support
<?php
use Four\MarketplaceHttp\Configuration\ClientConfig;
use Four\MarketplaceHttp\Factory\MarketplaceHttpClientFactory;
// Create factory
$factory = new MarketplaceHttpClientFactory();
// Create client with fluent configuration
$client = $factory->createClient(
ClientConfig::create('https://api.example.com')
->withAuth('bearer', 'your-token')
->withTimeout(30.0)
->withMiddleware(['logging', 'rate_limiting'])
->build()
);
// Make requests
$response = $client->request('GET', '/api/data');
$data = json_decode($response->getContent(), true);This library serves as the foundation for all Four Bytes marketplace clients:
// eBay Client with automatic rate limiting
use Four\Marketplaces\Ebay\ApiClient as EbayClient;
$ebayClient = new EbayClient($auth, 'production');
// Amazon Client with burst support
use Four\Marketplaces\Amazon\ApiClient as AmazonClient;
$amazonClient = new AmazonClient($auth, 'production');
// All clients automatically benefit from:
// - Unified rate limiting strategies
// - Consistent retry middleware
// - Advanced transport abstraction
// - Enterprise monitoring<?php
use Four\MarketplaceHttp\Configuration\ClientConfig;
use Four\MarketplaceHttp\Factory\MarketplaceHttpClientFactory;
$factory = new MarketplaceHttpClientFactory($logger, $cache);
// Create Amazon-optimized client
$amazonClient = $factory->createAmazonClient(
ClientConfig::create('https://sellingpartnerapi-eu.amazon.com')
->forAmazon() // Apply Amazon-specific defaults
->withAuth('bearer', $lwaAccessToken)
->withHeaders([
'x-amzn-marketplace-id' => 'A1PA6795UKMFR9'
])
->build()
);
// Fetch orders with automatic rate limiting
$response = $amazonClient->request('GET', '/orders/v0/orders', [
'query' => [
'MarketplaceIds' => 'A1PA6795UKMFR9',
'CreatedAfter' => '2025-01-01T00:00:00Z'
]
]);
$orders = json_decode($response->getContent(), true);<?php
use Four\MarketplaceHttp\Authentication\OAuth1aProvider;
use Four\MarketplaceHttp\Configuration\ClientConfig;
use Four\MarketplaceHttp\Factory\MarketplaceHttpClientFactory;
// Create OAuth 1.0a provider
$authProvider = OAuth1aProvider::discogs(
'your-consumer-key',
'your-consumer-secret',
'your-access-token',
'your-token-secret'
);
$factory = new MarketplaceHttpClientFactory();
// Create Discogs client with OAuth 1.0a
$discogsClient = $factory->createDiscogsClient(
ClientConfig::create('https://api.discogs.com')
->forDiscogs()
->withAuthentication($authProvider)
->build()
);
// Search releases
$response = $discogsClient->request('GET', '/database/search', [
'query' => [
'type' => 'release',
'artist' => 'Pink Floyd',
'title' => 'Dark Side of the Moon'
]
]);<?php
use Four\MarketplaceHttp\Configuration\ClientConfig;
$config = ClientConfig::create('https://api.marketplace.com')
// Authentication
->withAuth('bearer', 'your-token')
// Headers
->withUserAgent('MyApp/1.0')
->withAccept('application/json')
->withContentType('application/json')
// Timeouts and behavior
->withTimeout(45.0)
->withMaxRedirects(5)
// Middleware
->withMiddleware(['logging', 'rate_limiting', 'retry'])
// Marketplace presets
->forProduction() // or ->forDevelopment()
->build();// Amazon SP-API optimized
$amazonConfig = ClientConfig::create($amazonEndpoint)->forAmazon();
// eBay API optimized
$ebayConfig = ClientConfig::create($ebayEndpoint)->forEbay();
// Discogs API optimized
$discogsConfig = ClientConfig::create($discogsEndpoint)->forDiscogs();
// Bandcamp API optimized
$bandcampConfig = ClientConfig::create($bandcampEndpoint)->forBandcamp();The library provides different rate limiting strategies optimized for each marketplace:
<?php
use Four\MarketplaceHttp\Factory\MarketplaceHttpClientFactory;
$factory = new MarketplaceHttpClientFactory();
// Amazon: Token Bucket (20 req/sec with bursts)
$amazonLimiter = $factory->createRateLimiterFactory('amazon', [
'limit' => 20,
'rate' => ['interval' => '1 second', 'amount' => 20]
]);
// eBay: Fixed Window (5000 req/day)
$ebayLimiter = $factory->createRateLimiterFactory('ebay', [
'limit' => 5000,
'rate' => ['interval' => '1 day']
]);
// Discogs: Sliding Window (60 req/minute)
$discogsLimiter = $factory->createRateLimiterFactory('discogs', [
'limit' => 60,
'rate' => ['interval' => '1 minute']
]);Rate limits are automatically monitored and logged:
// Rate limit information is extracted from response headers:
// - Amazon: x-amzn-ratelimit-limit, x-amzn-ratelimit-remaining
// - eBay: x-ebay-api-analytics-daily-remaining
// - Discogs: x-discogs-ratelimit-remaining
// - Generic: x-ratelimit-limit, x-ratelimit-remaining<?php
use Four\MarketplaceHttp\Transport\TransportFactory;
// Get the best available transport
$transport = TransportFactory::createBest();
// Get marketplace-optimized transport
$amazonTransport = TransportFactory::createForMarketplace('amazon');
// Check available transports
$available = TransportFactory::getAvailable(); // ['curl', 'stream']
// Get system information
$sysInfo = TransportFactory::getSystemInfo();<?php
use Four\MarketplaceHttp\Transport\CurlTransport;
use Four\MarketplaceHttp\Transport\StreamTransport;
// cURL transport (recommended)
$curlTransport = new CurlTransport();
$response = $curlTransport->request('GET', 'https://api.example.com/data');
// Stream transport (fallback)
$streamTransport = new StreamTransport();
$response = $streamTransport->request('GET', 'https://api.example.com/data');<?php
use Four\MarketplaceHttp\Authentication\OAuthProvider;
// Amazon LWA OAuth
$amazonAuth = OAuthProvider::amazon(
'your-client-id',
'your-client-secret',
'your-refresh-token',
$httpClient,
$requestFactory,
$streamFactory
);
// eBay OAuth
$ebayAuth = OAuthProvider::ebay(
'your-client-id',
'your-client-secret',
'your-refresh-token',
$httpClient,
$requestFactory,
$streamFactory,
['https://api.ebay.com/oauth/api_scope']
);<?php
use Four\MarketplaceHttp\Authentication\OAuth1aProvider;
$discogsAuth = OAuth1aProvider::discogs(
'your-consumer-key',
'your-consumer-secret',
'your-access-token',
'your-token-secret'
);
// Test signature generation
$testResult = $discogsAuth->testSignature();<?php
// Bearer token
$config->withAuth('bearer', 'your-access-token');
// API key
$config->withAuth('api_key', 'your-api-key');
// Basic auth
$config->withAuth('basic', 'username:password');
// Custom token
$config->withAuth('token', 'your-token');- Logging: PSR-3 compatible request/response logging
- Rate Limiting: Automatic rate limit enforcement
- Retry: Exponential backoff retry logic
- Authentication: OAuth and token management
- Performance: Request timing and monitoring
- Caching: Response caching (planned)
<?php
use Four\MarketplaceHttp\Middleware\MiddlewareInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class CustomMiddleware implements MiddlewareInterface
{
public function wrap(HttpClientInterface $client): HttpClientInterface
{
return new CustomHttpClientWrapper($client);
}
public function getName(): string
{
return 'custom';
}
public function getPriority(): int
{
return 100;
}
}<?php
use Four\MarketplaceHttp\Exception\HttpClientException;
use Four\MarketplaceHttp\Exception\RateLimitException;
use Four\MarketplaceHttp\Exception\AuthenticationException;
use Four\MarketplaceHttp\Exception\RetryableException;
try {
$response = $client->request('GET', '/api/data');
} catch (RateLimitException $e) {
// Handle rate limiting (429 responses)
$waitTime = $e->getRetryAfter();
sleep($waitTime);
} catch (AuthenticationException $e) {
// Handle auth errors (401, 403)
$this->refreshToken();
} catch (RetryableException $e) {
// Handle temporary errors (500, 502, 503)
$this->retryLater($e);
} catch (HttpClientException $e) {
// Handle other HTTP errors
$this->logError($e);
}<?php
use Four\MarketplaceHttp\Configuration\ClientConfig;
$config = ClientConfig::create('https://api.example.com')
->withPerformanceMonitoring()
->build();
$client = $factory->createClient($config);
// Performance data is automatically collected:
// - Request/response times
// - Success/failure rates
// - Rate limit utilization
// - Error frequencies<?php
use Four\MarketplaceHttp\Tests\TestCase;
class MyApiTest extends TestCase
{
public function testApiCall(): void
{
$mockClient = $this->createMockClient([
$this->createJsonResponse(['status' => 'success']),
$this->createRateLimitResponse('amazon', 10, 8)
]);
// Test your API integration
$response = $mockClient->request('GET', '/test');
$this->assertSame(200, $response->getStatusCode());
}
}<?php
// Run tests
composer test
// Coverage report
composer test-coverage
// Quality analysis
composer phpstan
// Combined quality check
composer qualityThe examples/ directory contains comprehensive usage examples:
basic_usage.php- Getting started guideamazon_integration.php- Complete Amazon SP-API integrationrate_limiting_demo.php- Rate limiting strategies demonstrationoauth_examples.php- OAuth 1.0a and 2.0 authenticationtransport_comparison.php- Transport layer comparison
Run examples:
php examples/basic_usage.php
php examples/amazon_integration.php
php examples/rate_limiting_demo.php- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make changes with tests:
composer test - Ensure quality:
composer quality - Submit pull request
- PHP 8.4+ with strict typing
- PHPStan level 8 compliance
- 100% test coverage for new features
- PSR-12 coding standards
- Comprehensive documentation
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: Full API documentation available
- Issues: Report issues on GitHub
- Examples: Comprehensive examples provided
- Community: PHP community support
Initial Release
- ✅ Complete marketplace HTTP client factory
- ✅ Multiple transport methods (cURL, Stream)
- ✅ Advanced rate limiting strategies
- ✅ OAuth 1.0a and 2.0 authentication
- ✅ Comprehensive middleware system
- ✅ Amazon, eBay, Discogs, Bandcamp optimizations
- ✅ PHPUnit test suite with mocks
- ✅ PHPStan level 8 compliance
- ✅ Production-ready architecture
Four Bytes | Modern PHP Libraries for Marketplace Integration
Built with ❤️ for the PHP community