antelucangizmos/guzzle-retry-handler

test guzzle client with retry support

0.1.2 2024-06-19 20:45 UTC

This package is auto-updated.

Last update: 2025-06-19 22:58:23 UTC


README

Shields Shields

APIs might implement rate limiting, and if they do your clients might experience 429 Too Many Requests responses with a Retry-After header, informing your client how long it should wait before making the next request.

Guzzle includes a retry middleware class that can be used to handle this.

The implementation in this project is a PoC, so feel free to build upon it, and comment if you think something should be added / removed.

Example usage

require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Ag\Guzzle\Handler\Retry;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;

class TestRetry
{
    public function test(int $maxRetries = 5)
    {
        $handlerStack = HandlerStack::create(new CurlHandler());
        $retry = new Retry($maxRetries);
        $handlerStack->push(Middleware::retry($retry->retryDecider(), $retry->retryDelay()));

        $client = new Client(array('handler' => $handlerStack));

        $response = $client->request(
            'GET',
            // @ToDo replace to a real url!!!
            'https://some-url-here'
        )->getBody()->getContents();

        return print_r($response, true);
    }
}

try {
    $TestRetry = new TestRetry();
    $response = $TestRetry->test(5);
    echo $response . PHP_EOL;
} catch (ConnectException $e) {
    echo $e->getMessage() . PHP_EOL;
}