PHP code example of poor-plebs / guzzle-connect-retry-decider

1. Go to this page and download the library: Download poor-plebs/guzzle-connect-retry-decider 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/ */

    

poor-plebs / guzzle-connect-retry-decider example snippets



use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

$handlerStack = HandlerStack::create();

// Where to put this middleware in the middleware stack depends on your usecase.
// Usually just before the handler on top or before a log middleware.
$handlerStack->push(
    Middleware::retry(new ConnectRetryDecider(
        maxRetries: 3,
        onBeforeRetry: function (
            int $retries,
            RequestInterface $request,
            Throwable $exception
        ): void {
            /* Optional closure that is executed just before the retry is done.
             * At this point it is already decided that we will retry.
             *
             * Can be used to log the following retry or do some other action.
             */
        }
    )),
    'connect_retry',
);

$client = new Client([
    'base_uri' => 'https://sometest.com/',
    'handler' => $handlerStack,
]);

$client->getAsync('information')->wait();