Redirects
Guzzle will automatically follow redirects unless you tell it not to. You can customize the redirect behavior using the allow_redirects
request option.
- Set to
true
to enable normal redirects with a maximum number of 5 redirects. This is the default setting. - Set to
false
to disable redirects. - Pass an associative array containing the ‘max’ key to specify the maximum number of redirects and optionally provide a ‘strict’ key value to specify whether or not to use strict RFC compliant redirects (meaning redirect POST requests with POST requests vs. doing what most browsers do which is redirect POST requests with GET requests).
$response = $client->request('GET', 'http://github.com');
echo $response->getStatusCode();
// 200
The following example shows that redirects can be disabled.
$response = $client->request('GET', 'http://github.com', [
'allow_redirects' => false
]);
echo $response->getStatusCode();
// 301