3

Following code throws an error "paremeter 4 &$options expected to be a reference, value given"

This code is from guzzle_retry_middleware#on-retry-callback

I wanted to stop the guzzle retry call after receiving self::STATUS_DRAFT === $document->status with response 200

$listener = function(int $attemptNumber, float $delay, RequestInterface $request, array &$options, ?ResponseInterface $response)
{
    $document = \json_decode();

    if (self::STATUS_DRAFT === $document->status)
    {
        return $options['retry_enabled'] = false;
    }
};

$stack = HandlerStack::create();
$stack->push(GuzzleRetryMiddleware::factory([
    'on_retry_callback' => $listener,
    'retry_on_status'   =>[429, 503, 200]
]));

$response = $guzzleClient->get($url, ['handler' => $stack]);
Safry
  • 191
  • 1
  • 9
  • 1
    On `retry_on_status` you have included 200, which means it will retry for the status code 200. Have you tried removing the 200 status code from that? – Shifrin Jul 27 '22 at 05:23
  • it should call event in the 200 request that is my requirement. – Safry Jul 27 '22 at 05:30

1 Answers1

2

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 retrying
Received 68 so retrying
Received 31 so stopping

Here's my composer.json file

{
    "require": {
        "guzzlehttp/guzzle": "^7.0",
        "caseyamcl/guzzle_retry_middleware": "^2.7"
    }
}
JohnP
  • 49,507
  • 13
  • 108
  • 140