The phasync HTTP Client is a powerful, fiber-based HTTP client for PHP, leveraging the concurrency features of PHP Fibers to manage non-blocking requests efficiently. This client is compliant with PSR-18 and allows extensive configuration via cURL options to tailor request handling according to your needs.
- Concurrent HTTP requests: Utilizes PHP fibers to perform non-blocking HTTP requests.
- Full PSR-18 compatibility: Fully compliant with the PSR-18 interface for HTTP clients.
- Extensive configuration: Customize every aspect of HTTP requests using a wide range of cURL options.
Use Composer to install the Phasync HTTP Client in your project:
composer require phasync/http-client
Here is a simple example of making concurrent GET request:
use phasync\HttpClient\HttpClient;
$client = new HttpClient();
// Concurrent requests:
$response1 = $client->get('https://httpbin.org/get');
$response2 = $client->get('https://www.reddit.com/");
// All requests are performed in parallel using `curl_multi` under the hood.
echo $response1->getBody();
echo $resposne2->getBody();
To send a POST request with data:
$response = $client->post('https://httpbin.org/post', [
'foo' => 'bar'
]);
echo $response->getBody();
The client supports the PSR-18 client specification:
$psr7Response = $client->sendRequest($psr7Request);
Automatically handle redirects:
$client = new HttpClient([
'followLocation' => true
]);
$response = $client->get('https://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com');
Customize client behavior by setting cURL options:
$client = new HttpClient([
'timeoutMs' => 1000,
'userAgent' => 'PhasyncClient/1.0'
]);
The HttpClientOptions
class provides a way to configure a variety of options for handling requests:
userAgent
: Set the 'User-Agent' header.timeoutMs
: Maximum number of milliseconds to allow cURL functions to execute.followLocation
: Follow redirects.sslVerifyPeer
: Verify the peer's SSL certificate.- And many more detailed in the class definition.
Refer to the HttpClientOptions
class for a comprehensive list of all configurable options.
Contributions are welcome! Please feel free to submit pull requests or create issues for bugs and feature requests.
This project is open-sourced software licensed under the MIT license.