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 maximumnumber 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 valuepairs:

  • 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 asPOST requests vs. doing what most browsers do which is redirect POST requestswith GET requests.

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

  • protocols: (array, default=['http', 'https']) Specified which protocols areallowed for redirect requests.

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

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

Note: When tracking redirects the X-Guzzle-Redirect-History header willexclude the initial request's URI and the X-Guzzle-Redirect-Status-Historyheader 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.  
  5. $onRedirect = function(
  6. RequestInterface $request,
  7. ResponseInterface $response,
  8. UriInterface $uri
  9. ) {
  10. echo 'Redirecting! ' . $request->getUri() . ' to ' . $uri . "\n";
  11. };
  12.  
  13. $res = $client->request('GET', '/redirect/3', [
  14. 'allow_redirects' => [
  15. 'max' => 10, // allow at most 10 redirects.
  16. 'strict' => true, // use "strict" RFC compliant redirects.
  17. 'referer' => true, // add a Referer header
  18. 'protocols' => ['https'], // only allow https URLs
  19. 'on_redirect' => $onRedirect,
  20. 'track_redirects' => true
  21. ]
  22. ]);
  23.  
  24. echo $res->getStatusCode();
  25. // 200
  26.  
  27. echo $res->getHeaderLine('X-Guzzle-Redirect-History');
  28. // http://first-redirect, http://second-redirect, etc...
  29.  
  30. echo $res->getHeaderLine('X-Guzzle-Redirect-Status-History');
  31. // 301, 302, etc...

Warning

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