small/swoole-symfony-http-client

Swoole-based Symfony HTTP client implementation that conforms to the Symfony HttpClientInterface for high-performance asynchronous HTTP requests

1.1.0 2024-10-03 17:28 UTC

This package is auto-updated.

Last update: 2024-11-03 17:41:27 UTC


README

Small Swoole Symfony Http Client

                

Small Swoole Symfony Http Client is a custom HTTP client built using Swoole, designed to be fully compatible with Symfony's HttpClient Component. It provides an asynchronous, non-blocking HTTP client with support for various features such as redirections, retries, timeouts, and more.

Requirements

  • PHP 8.3 or higher
  • Swoole 5.x or higher or OpenSwoole 22.1.2 or higher
  • Composer

Installation

First, ensure you have Swoole installed on your system. You can install it via PECL:

pecl install swoole

Next, install SwooleHttpClient and its dependencies via Composer:

composer require small/swoole-symfony-http-client

Usage

Basic GET Request

use Small\SwooleSymfonyHttpClient\SwooleHttpClient;

$client = new SwooleHttpClient();
$response = $client->request('GET', 'https://example.com');

echo $response->getContent();

Basic POST Request

$client = new SwooleHttpClient();
$response = $client->request('POST', 'https://example.com/api', [
'body' => ['key' => 'value']
]);

echo $response->getContent();

Sending JSON Data

$client = new SwooleHttpClient();
$response = $client->request('PUT', 'https://example.com/api', [
'json' => ['name' => 'John', 'age' => 30]
]);

echo $response->getContent();

Handling Timeouts

$client = new SwooleHttpClient();
try {
$response = $client->request('GET', 'https://example.com/slow-endpoint', [
'timeout' => 2 // in seconds
]);
} catch (Small\SwooleSymfonyHttpClient\Exception\TimeoutException $e) {
echo "Request timed out!";
}

Handling Redirections

$client = new SwooleHttpClient();
$response = $client->withOptions(['max_redirects' => 3])
->request('GET', 'https://example.com/redirect');

echo $response->getContent();

Basic Authentication

$client = new SwooleHttpClient();
$response = $client->withOptions([
'auth_basic' => ['username' => 'admin', 'password' => 'password']
])->request('GET', 'https://example.com/auth');

echo $response->getContent();

Bearer Token Authentication

$client = new SwooleHttpClient();
$response = $client->withOptions([
'auth_bearer' => 'your-bearer-token'
])->request('GET', 'https://example.com/protected');

echo $response->getContent();

Retries on Failed Requests

$client = new SwooleHttpClient();
$response = $client->withOptions([
'retry_failed' => 3 // Retry up to 3 times on failure
])->request('GET', 'https://example.com/flaky-endpoint');

echo $response->getContent();

Custom Headers

$client = new SwooleHttpClient();
$response = $client->withOptions([
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer token'
]
])->request('GET', 'https://example.com/api');

echo $response->getContent();

Running Tests

Build containers :

docker compose up -d

And run with composer :

bin/composer unit-tests

License

This project is licensed under the MIT License. See the LICENSE file for details.