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 maximum number of 5 redirects.
$res = $client->request('GET', '/redirect/3');
echo $res->getStatusCode();
// 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 theX-Guzzle-Redirect-History
andX-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 theX-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 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\HandlerStack::create
.