reaway / http-client
HTTP Client implementation using PHP cURL with PSR-7, PSR-17, PSR-18 support
v1.0.0
2026-03-18 08:57 UTC
Requires
- php: >=8.0
- ext-curl: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0|^2.0
Requires (Dev)
- phpunit/phpunit: ^9.0|^10.0
README
A lightweight HTTP client library based on PHP cURL, supporting PSR-7, PSR-17, and PSR-18 standards.
Features
- ✅ PSR-7 (HTTP Messages) compatible
- ✅ PSR-17 (HTTP Factories) compatible
- ✅ PSR-18 (HTTP Client) compatible
- ✅ Simple and easy-to-use API
- ✅ Support for GET, POST, JSON, and file upload requests
- ✅ Custom request headers
- ✅ Query parameter support
- ✅ Stream-based file upload (suitable for large files)
Installation
composer require reaway/http-client
Requirements
- PHP >= 8.0
- ext-curl
Quick Start
<?php use HttpClient\HttpClient; use HttpClient\Psr17\RequestFactory; use HttpClient\Psr17\StreamFactory; use HttpClient\Psr18\Client; // Create HttpClient instance $client = new HttpClient( new Client(), // PSR-18 ClientInterface new RequestFactory(), // PSR-17 RequestFactoryInterface new StreamFactory() // PSR-17 StreamFactoryInterface ); // Send GET request $response = $client->get('https://api.example.com/users'); echo $response->getBody();
Usage
GET Request
// Simple GET request $response = $client->get('https://api.example.com/users'); // With query parameters $response = $client->get( 'https://api.example.com/users', ['page' => 1, 'limit' => 10], ['Authorization' => 'Bearer token'] );
POST Request (Form Data)
$response = $client->postForm( 'https://api.example.com/users', [ 'name' => 'John Doe', 'email' => 'john@example.com' ], ['Authorization' => 'Bearer token'] );
POST Request (JSON)
$response = $client->postJson( 'https://api.example.com/users', [ 'name' => 'John Doe', 'email' => 'john@example.com', 'address' => [ 'city' => 'New York', 'street' => '5th Avenue' ] ], ['Authorization' => 'Bearer token'] );
POST 请求(multipart/form-data)
$response = $client->postMultipart( 'https://api.example.com/upload', // Regular form fields ['description' => 'File upload test'], // File fields - Full format [ 'avatar' => [ 'path' => '/path/to/avatar.jpg', 'filename' => 'avatar.jpg', 'type' => 'image/jpeg' ], 'document' => [ 'path' => '/path/to/document.pdf', 'filename' => 'report.pdf', 'type' => 'application/pdf' ] ], ['Authorization' => 'Bearer token'] ); // Shorthand format for file fields $response = $client->postMultipart( 'https://api.example.com/upload', [], [ 'file' => ['/path/to/file.zip', 'archive.zip', 'application/zip'] ] );
Set Default Headers
$client->setDefaultHeaders([ 'User-Agent' => 'MyApp/1.0', 'X-API-Key' => 'your-api-key' ]);
Send Raw Request
use Psr\Http\Message\RequestInterface; $request = $requestFactory->createRequest('GET', 'https://api.example.com/users'); $response = $client->send($request);
API Reference
HttpClient Methods
| Method | Description |
|---|---|
setDefaultHeaders(array $headers): self |
Set default request headers |
get(string $url, array $headers = [], array $queryParams = []): ResponseInterface |
Send GET request |
post(string $url, array $data = [], array $headers = []): ResponseInterface |
Send POST request (form data) |
postJson(string $url, array|object $data, array $headers = []): ResponseInterface |
Send POST request (JSON) |
upload(string $url, array $fields = [], array $files = [], array $headers = []): ResponseInterface |
Send multipart/form-data request (file upload) |
send(RequestInterface $request): ResponseInterface |
Send raw PSR-7 request |
Testing
./vendor/bin/phpunit
License
This project is licensed under the MIT License.