allow_redirects

Summary

Describes the redirect behavior of a request

Types

  • bool
  • array

Default

  1. [
  2. 'max' => 5,
  3. 'strict' => false,
  4. 'referer' => false,
  5. 'protocols' => ['http', 'https'],
  6. 'track_redirects' => false
  7. ]

Constant

GuzzleHttp\RequestOptions::ALLOW_REDIRECTS

Set to false to disable redirects.

  1. $res = $client->request('GET', '/redirect/3', ['allow_redirects' => false]);
  2. echo $res->getStatusCode();
  3. // 302

Set to true (the default setting) to enable normal redirects with a maximum number of 5 redirects.

  1. $res = $client->request('GET', '/redirect/3');
  2. echo $res->getStatusCode();
  3. // 200

You can also pass an associative array containing the following key value pairs:

  • max: (int, default=5) maximum number of allowed redirects.

  • strict: (bool, default=false) Set to true to use strict redirects. Strict RFC compliant redirects mean that POST redirect requests are sent as POST requests vs. doing what most browsers do which is redirect POST requests with GET requests.

  • referer: (bool, default=false) Set to true to enable adding the Referer header when redirecting.

  • protocols: (array, default=[‘http’, ‘https’]) Specified which protocols are allowed for redirect requests.

  • on_redirect: (callable) PHP callable that is invoked when a redirect is encountered. The callable is invoked with the original request and the redirect response that was received. Any return value from the on_redirect function is ignored.

  • track_redirects: (bool) When set to true, each redirected URI and status code encountered will be tracked in the X-Guzzle-Redirect-History and X-Guzzle-Redirect-Status-History headers respectively. All URIs and status codes will be stored in the order which the redirects were encountered.

    Note: When tracking redirects the X-Guzzle-Redirect-History header will exclude the initial request’s URI and the X-Guzzle-Redirect-Status-History header will exclude the final status code.

  1. use Psr\Http\Message\RequestInterface;
  2. use Psr\Http\Message\ResponseInterface;
  3. use Psr\Http\Message\UriInterface;
  4. $onRedirect = function(
  5. RequestInterface $request,
  6. ResponseInterface $response,
  7. UriInterface $uri
  8. ) {
  9. echo 'Redirecting! ' . $request->getUri() . ' to ' . $uri . "\n";
  10. };
  11. $res = $client->request('GET', '/redirect/3', [
  12. 'allow_redirects' => [
  13. 'max' => 10, // allow at most 10 redirects.
  14. 'strict' => true, // use "strict" RFC compliant redirects.
  15. 'referer' => true, // add a Referer header
  16. 'protocols' => ['https'], // only allow https URLs
  17. 'on_redirect' => $onRedirect,
  18. 'track_redirects' => true
  19. ]
  20. ]);
  21. echo $res->getStatusCode();
  22. // 200
  23. echo $res->getHeaderLine('X-Guzzle-Redirect-History');
  24. // http://first-redirect, http://second-redirect, etc...
  25. echo $res->getHeaderLine('X-Guzzle-Redirect-Status-History');
  26. // 301, 302, etc...

Warning

This option only has an effect if your handler has the GuzzleHttp\Middleware::redirect middleware. This middleware is added by default when a client is created with no handler, and is added by default when creating a handler with GuzzleHttp\default_handler.