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).
  1. $response = $client->request('GET', 'http://github.com');
  2. echo $response->getStatusCode();
  3. // 200

The following example shows that redirects can be disabled.

  1. $response = $client->request('GET', 'http://github.com', [
  2. 'allow_redirects' => false
  3. ]);
  4. echo $response->getStatusCode();
  5. // 301