PHP code example of compwright / should-retry

1. Go to this page and download the library: Download compwright/should-retry 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/ */

    

compwright / should-retry example snippets


use Compwright\ShouldRetry\ShouldRetry;
use Compwright\ShouldRetry\RetryAfter;

$handler = GuzzleHttp\HandlerStack::create();

$handler->push(
    GuzzleHttp\Middleware::retry(
        new ShouldRetry(),
        new RetryAfter()
    ),
    'retry'
);

$client = new GuzzleHttp\Client([
    'handler' => $handler,
]);

$shouldRetry = (new ShouldRetry())
    ->setMaxRetries(5)
    ->setRetryOnStatusCodes(429)
    ->setLogger($logger); // attach PSR-3 debug logger

$retryAfter = (new RetryAfter())
    ->setRetryAfterHeader('x-rate-limit-reset')
    ->setFallbackStrategy($fallback);