allow_redirects
- Summary
Describes the redirect behavior of a request- Types
-
- bool
- array
- Default
-
- [
- 'max' => 5,
- 'strict' => false,
- 'referer' => false,
- 'protocols' => ['http', 'https'],
- 'track_redirects' => false
- ]
- Constant
GuzzleHttp\RequestOptions::ALLOW_REDIRECTS
Set to
false
to disable redirects.
- $res = $client->request('GET', '/redirect/3', ['allow_redirects' => false]);
- echo $res->getStatusCode();
- // 302
Set to true
(the default setting) to enable normal redirects with a maximumnumber of 5 redirects.
- $res = $client->request('GET', '/redirect/3');
- echo $res->getStatusCode();
- // 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 theX-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-History
header will exclude the final status code.
- use Psr\Http\Message\RequestInterface;
- use Psr\Http\Message\ResponseInterface;
- use Psr\Http\Message\UriInterface;
- $onRedirect = function(
- RequestInterface $request,
- ResponseInterface $response,
- UriInterface $uri
- ) {
- echo 'Redirecting! ' . $request->getUri() . ' to ' . $uri . "\n";
- };
- $res = $client->request('GET', '/redirect/3', [
- 'allow_redirects' => [
- 'max' => 10, // allow at most 10 redirects.
- 'strict' => true, // use "strict" RFC compliant redirects.
- 'referer' => true, // add a Referer header
- 'protocols' => ['https'], // only allow https URLs
- 'on_redirect' => $onRedirect,
- 'track_redirects' => true
- ]
- ]);
- echo $res->getStatusCode();
- // 200
- echo $res->getHeaderLine('X-Guzzle-Redirect-History');
- // http://first-redirect, http://second-redirect, etc...
- echo $res->getHeaderLine('X-Guzzle-Redirect-Status-History');
- // 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
.