A simple, Laravel-inspired HTTP client abstraction built on top of Symfony HttpClient. This library provides a fluent, developer-friendly interface for making HTTP requests in PHP applications.
- 🚀 Simple & Intuitive API - Fluent interface for building requests
- 🔄 Static & Instance Methods - Use whichever style fits your needs
- 🔐 Built-in Authentication - Bearer tokens and Basic auth support
- 📦 JSON Handling - Automatic encoding/decoding with convenience methods
- ⚡ Retry Logic - Configurable retry strategies with exponential backoff
- ⏱️ Timeout Control - Set request timeouts easily
- 🛡️ Error Handling - Graceful error handling with custom exceptions
- 🎯 Response Helpers - Convenient methods for checking status and accessing data
Install via Composer:
composer require elliephp/httpclient- PHP 8.4 or higher
- Symfony HttpClient component
For quick, one-off requests, use static methods:
use ElliePHP\Components\HttpClient\HttpClient;
// GET request
$response = HttpClient::get('https://api.example.com/users');
// POST request
$response = HttpClient::post('https://api.example.com/users', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);
// Check response
if ($response->successful()) {
$data = $response->json();
echo "User created: " . $data['name'];
}For multiple requests with shared configuration, create an instance:
$client = new HttpClient();
$response = $client
->withBaseUrl('https://api.example.com')
->withToken('your-api-token')
->acceptJson()
->get('/users');// Simple GET
$response = HttpClient::get('https://api.example.com/users');
// GET with query parameters
$response = HttpClient::get('https://api.example.com/users', [
'page' => 1,
'limit' => 10
]);// POST with form data
$response = HttpClient::post('https://api.example.com/users', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);
// POST with JSON
$client = new HttpClient();
$response = $client
->asJson()
->post('https://api.example.com/users', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);$response = HttpClient::put('https://api.example.com/users/123', [
'name' => 'Jane Doe'
]);$response = HttpClient::patch('https://api.example.com/users/123', [
'status' => 'active'
]);$response = HttpClient::delete('https://api.example.com/users/123');$client = new HttpClient();
$response = $client
->withToken('your-api-token')
->get('https://api.example.com/protected-resource');$client = new HttpClient();
$response = $client
->withBasicAuth('username', 'password')
->get('https://api.example.com/protected-resource');$client = new HttpClient();
// asJson() sets Content-Type header and encodes body as JSON
$response = $client
->asJson()
->post('https://api.example.com/users', [
'name' => 'John Doe',
'email' => 'john@example.com',
'metadata' => [
'role' => 'admin',
'department' => 'IT'
]
]);$response = HttpClient::get('https://api.example.com/users/123');
// Get entire JSON response as array
$data = $response->json();
echo $data['name']; // John Doe
// Get specific key from JSON
$name = $response->json('name');
echo $name; // John Doe
// Handle invalid JSON gracefully
$data = $response->json(); // Returns null if JSON is invalid$client = new HttpClient();
// Sets Accept: application/json header
$response = $client
->acceptJson()
->get('https://api.example.com/users');$client = new HttpClient();
// Set base URL for all requests
$response = $client
->withBaseUrl('https://api.example.com')
->get('/users'); // Requests https://api.example.com/users
// Absolute URLs override base URL
$response = $client
->withBaseUrl('https://api.example.com')
->get('https://other-api.com/data'); // Requests https://other-api.com/data$client = new HttpClient();
// Add multiple headers
$response = $client
->withHeaders([
'X-API-Key' => 'secret-key',
'User-Agent' => 'MyApp/1.0',
'X-Custom-Header' => 'value'
])
->get('https://api.example.com/data');$client = new HttpClient();
// Set timeout in seconds
$response = $client
->withTimeout(30)
->get('https://api.example.com/slow-endpoint');Configure automatic retry behavior for failed requests:
$client = new HttpClient();
$response = $client
->withRetry([
'max_retries' => 3, // Retry up to 3 times
'delay' => 1000, // Start with 1 second delay (milliseconds)
'multiplier' => 2, // Double delay each time: 1s, 2s, 4s
'max_delay' => 10000, // Cap delay at 10 seconds
])
->get('https://api.example.com/data');$response = $client
->withRetry([
'max_retries' => 5,
'delay' => 2000, // 2 second delay
'multiplier' => 1, // Keep delay constant
])
->get('https://api.example.com/data');Add randomness to prevent thundering herd:
$response = $client
->withRetry([
'max_retries' => 3,
'delay' => 1000,
'multiplier' => 2,
'jitter' => 0.1, // Add ±10% random variation
])
->get('https://api.example.com/data');$response = $client
->withRetry([
'max_retries' => 3,
'delay' => 1000,
'multiplier' => 2,
'http_codes' => [429, 500, 502, 503, 504], // Only retry these codes
])
->get('https://api.example.com/data');Pass any Symfony HttpClient options directly:
$client = new HttpClient();
$response = $client
->withOptions([
'max_redirects' => 5,
'timeout' => 30,
'verify_peer' => true,
'verify_host' => true,
])
->get('https://api.example.com/data');For all available options, see the Symfony HttpClient documentation.
$response = HttpClient::get('https://api.example.com/users');
// Check if successful (2xx status)
if ($response->successful()) {
echo "Request succeeded!";
}
// Check if failed (4xx or 5xx status)
if ($response->failed()) {
echo "Request failed!";
}
// Get status code
$status = $response->status(); // e.g., 200, 404, 500$response = HttpClient::get('https://api.example.com/users');
// Get raw body
$body = $response->body();
// Get JSON data
$data = $response->json();
// Get specific JSON key
$name = $response->json('name');$response = HttpClient::get('https://api.example.com/users');
// Get all headers
$headers = $response->headers();
// Get specific header
$contentType = $response->header('Content-Type');
$rateLimit = $response->header('X-RateLimit-Remaining');The library throws RequestException for network errors and timeouts:
use ElliePHP\Components\HttpClient\HttpClient;
use ElliePHP\Components\HttpClient\RequestException;
try {
$response = HttpClient::get('https://api.example.com/users');
if ($response->successful()) {
$data = $response->json();
// Process data
} else {
// Handle 4xx/5xx responses
echo "HTTP Error: " . $response->status();
}
} catch (RequestException $e) {
// Handle network errors, timeouts, etc.
echo "Request failed: " . $e->getMessage();
// Access original exception if needed
$previous = $e->getPrevious();
}- Network Errors: Connection failures, DNS resolution errors, SSL errors
- Timeout Errors: Request exceeds configured timeout
- Transport Errors: Other Symfony transport-level errors
Note: 4xx and 5xx HTTP responses do NOT throw exceptions by default. Use $response->successful() or $response->failed() to check status.
All configuration methods return a ClientBuilder instance, allowing fluent method chaining:
$client = new HttpClient();
$response = $client
->withBaseUrl('https://api.example.com')
->withToken('your-api-token')
->withTimeout(30)
->withRetry([
'max_retries' => 3,
'delay' => 1000,
'multiplier' => 2,
])
->acceptJson()
->asJson()
->post('/users', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);use ElliePHP\Components\HttpClient\HttpClient;
// Quick one-off requests
$users = HttpClient::get('https://api.example.com/users')->json();
foreach ($users as $user) {
echo $user['name'] . "\n";
}use ElliePHP\Components\HttpClient\HttpClient;
use ElliePHP\Components\HttpClient\RequestException;
class ApiClient
{
private HttpClient $client;
public function __construct(string $apiToken)
{
$this->client = new HttpClient();
}
public function getUsers(int $page = 1): array
{
try {
$response = $this->client
->withBaseUrl('https://api.example.com')
->withToken($apiToken)
->withTimeout(30)
->acceptJson()
->get('/users', ['page' => $page]);
if ($response->successful()) {
return $response->json();
}
throw new \Exception('Failed to fetch users: ' . $response->status());
} catch (RequestException $e) {
throw new \Exception('API request failed: ' . $e->getMessage(), 0, $e);
}
}
public function createUser(array $userData): array
{
try {
$response = $this->client
->withBaseUrl('https://api.example.com')
->withToken($apiToken)
->asJson()
->post('/users', $userData);
if ($response->successful()) {
return $response->json();
}
throw new \Exception('Failed to create user: ' . $response->status());
} catch (RequestException $e) {
throw new \Exception('API request failed: ' . $e->getMessage(), 0, $e);
}
}
}use ElliePHP\Components\HttpClient\HttpClient;
$client = new HttpClient();
// Configure for resilient API calls
$response = $client
->withBaseUrl('https://api.example.com')
->withToken('your-api-token')
->withTimeout(30)
->withRetry([
'max_retries' => 3,
'delay' => 1000,
'multiplier' => 2,
'jitter' => 0.1,
'http_codes' => [429, 500, 502, 503, 504],
])
->acceptJson()
->asJson()
->post('/orders', [
'product_id' => 123,
'quantity' => 2,
'customer_id' => 456
]);
if ($response->successful()) {
$order = $response->json();
echo "Order created: " . $order['id'];
} else {
echo "Order failed: " . $response->status();
}HttpClient::get(string $url, array $query = []): ResponseHttpClient::post(string $url, array $data = []): ResponseHttpClient::put(string $url, array $data = []): ResponseHttpClient::patch(string $url, array $data = []): ResponseHttpClient::delete(string $url): Response
withBaseUrl(string $baseUrl): ClientBuilderwithHeaders(array $headers): ClientBuilderwithToken(string $token): ClientBuilderwithBasicAuth(string $username, string $password): ClientBuilderacceptJson(): ClientBuilderasJson(): ClientBuilderwithTimeout(int $seconds): ClientBuilderwithRetry(array $retryConfig): ClientBuilderwithOptions(array $options): ClientBuilder
get(string $url, array $query = []): Responsepost(string $url, array $data = []): Responseput(string $url, array $data = []): Responsepatch(string $url, array $data = []): Responsedelete(string $url): Response
status(): int- Get HTTP status codesuccessful(): bool- Check if status is 2xxfailed(): bool- Check if status is 4xx or 5xx
body(): string- Get raw response bodyjson(?string $key = null): mixed- Decode JSON responseheaders(): array- Get all response headersheader(string $name): ?string- Get specific header
Custom exception thrown for network errors, timeouts, and transport failures.
try {
$response = HttpClient::get('https://api.example.com/data');
} catch (RequestException $e) {
echo $e->getMessage(); // Error message
echo $e->getCode(); // Error code
$previous = $e->getPrevious(); // Original exception
}Run the test suite:
composer testRun tests with coverage:
composer test:coverageThis library is open-sourced software licensed under the MIT license.
Contributions are welcome! Please feel free to submit a Pull Request.
- Issues: GitHub Issues
- Source: GitHub Repository
Built on top of Symfony HttpClient.