PHP code example of shoxcie / batch-http-client

1. Go to this page and download the library: Download shoxcie/batch-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shoxcie / batch-http-client example snippets


use Shoxcie\BatchHttpClient\BatchHttpClient;
use Shoxcie\BatchHttpClient\RequestConfig;

$client = new BatchHttpClient();

$results = $client
    ->request([
        'users' => new RequestConfig('GET', 'https://api.example.com/users'),
        'orders' => new RequestConfig('POST', 'https://api.example.com/orders', options: [
            'json' => ['item' => 'widget', 'qty' => 3],
        ]),
    ])
    ->fetch();

$results['users'];  // decoded JSON array
$results['orders']; // decoded JSON array

$results = $client
    ->request([
        'flaky' => new RequestConfig('GET', 'https://api.example.com/flaky',
            maxRetries: 3,
        ),
    ])
    ->fetch();

new RequestConfig('GET', 'https://api.example.com/resource',
    maxRetries: 2,
    retryOptions: ['timeout' => 30],
)

new RequestConfig('GET', 'https://api.example.com/resource',
    maxRetries: 3,
    retryOptions: function (string $key, int $retries, ExceptionInterface|InvalidResponseException $e): array {
        return ['timeout' => 10 * $retries];
    },
)

$results = $client
    ->request([...])
    ->onSuccess(function (string $key, int $retries, mixed $result, ResponseInterface $response) {
        // called for each 2xx response, after parseResponse if configured
        // $retries = 0 on first-attempt success, N on success after N retries
    })
    ->onRetry(function (string $key, int $retries, ResponseInterface $failedResponse, ExceptionInterface|InvalidResponseException $e, ResponseInterface $retryResponse) {
        // called when a retry fires; $retries is the retry count after this retry fired (always >= 1)
    })
    ->onExhausted(function (string $key, int $retries, ResponseInterface $response, ExceptionInterface|InvalidResponseException $e) {
        // called when a single request exhausts all retries
        // $retries equals maxRetries normally; less when a transport error short-circuits
    })
    ->onAbort(function (string $key, int $retries, ResponseInterface $response, Throwable $e) {
        // called when an unexpected exception (e.g. throwing user callback) cancels the whole batch
        // $retries is the retry count for the request being processed when the abort fired
    })
    ->fetch();

new RequestConfig('GET', 'https://api.example.com/critical',
    maxRetries: 3,
    throwOnExhausted: true, // default
)

new RequestConfig('GET', 'https://api.example.com/optional',
    throwOnExhausted: false,
)

new RequestConfig('GET', 'https://example.com/file.csv',
    decodeJson: false,
)

new RequestConfig('GET', 'https://api.example.com/users',
    parseResponse: fn(string $key, mixed $result, ResponseInterface $response): mixed
        => $result['data'],
)

use Shoxcie\BatchHttpClient\InvalidResponseException;

new RequestConfig('GET', 'https://api.example.com/job-status',
    maxRetries: 5,
    parseResponse: function (string $key, mixed $result): mixed {
        if ($result['status'] === 'pending') {
            throw new InvalidResponseException('job not finished');
        }

        return $result;
    },
)

use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create([
    'timeout' => 10,
    'max_duration' => 30,
]);

$client = new BatchHttpClient($httpClient);