Quantcast
Channel: User JohnP - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Answer by JohnP for how to stop retry HTTP call when server servers respond with 200 status codes using guzzle retry middleware?

$
0
0

This test code seems to work fine for me. I created a request that just randomly spits out a number, which is used in the retry callback.

I'm using PHP 7.3 and the latest version of Guzzle and the middleware. If you're getting an error about the $options variable, try isolating this code on your environment and confirm there's no version mismatch.

use GuzzleHttp\Client;use GuzzleHttp\HandlerStack;use GuzzleRetry\GuzzleRetryMiddleware;use Psr\Http\Message\RequestInterface;use Psr\Http\Message\ResponseInterface;error_reporting(E_ALL);ini_set('display_errors', '1');$stack = HandlerStack::create();$stack->push(GuzzleRetryMiddleware::factory());$listener = function(int $attemptNumber, float $delay, RequestInterface &$request, array &$options, ResponseInterface $response) {    $doc = json_decode($response->getBody()->getContents());    echo "<br>Received {$doc->status} so ";    if ($doc->status < 50) {        $options['retry_enabled'] = false;        echo "stopping";        return;    }    echo "retrying";};$client = new Client(['handler' => $stack,'on_retry_callback' => $listener,'retry_on_status'   => [200]]);$response = $client->get('https://eouqfts30zafzos.m.pipedream.net/');

And I get this response

Received 69 so retryingReceived 68 so retryingReceived 31 so stopping

Here's my composer.json file

{"require": {"guzzlehttp/guzzle": "^7.0","caseyamcl/guzzle_retry_middleware": "^2.7"    }}

Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>