http-redirects.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ==============
  2. HTTP redirects
  3. ==============
  4. By default, Guzzle will automatically follow redirects using the non-RFC compliant implementation used by most web
  5. browsers. This means that redirects for POST requests are followed by a GET request. You can force RFC compliance by
  6. enabling the strict mode on a request's parameter object:
  7. .. code-block:: php
  8. // Set per request
  9. $request = $client->post();
  10. $request->getParams()->set('redirect.strict', true);
  11. // You can set globally on a client so all requests use strict redirects
  12. $client->getConfig()->set('request.params', array(
  13. 'redirect.strict' => true
  14. ));
  15. By default, Guzzle will redirect up to 5 times before throwing a ``Guzzle\Http\Exception\TooManyRedirectsException``.
  16. You can raise or lower this value using the ``redirect.max`` parameter of a request object:
  17. .. code-block:: php
  18. $request->getParams()->set('redirect.max', 2);
  19. Redirect history
  20. ----------------
  21. You can get the number of redirects of a request using the resulting response object's ``getRedirectCount()`` method.
  22. Similar to cURL's ``effective_url`` property, Guzzle provides the effective URL, or the last redirect URL that returned
  23. the request, in a response's ``getEffectiveUrl()`` method.
  24. When testing or debugging, it is often useful to see a history of redirects for a particular request. This can be
  25. achieved using the HistoryPlugin.
  26. .. code-block:: php
  27. $request = $client->get('/');
  28. $history = new Guzzle\Plugin\History\HistoryPlugin();
  29. $request->addSubscriber($history);
  30. $response = $request->send();
  31. // Get the last redirect URL or the URL of the request that received
  32. // this response
  33. echo $response->getEffectiveUrl();
  34. // Get the number of redirects
  35. echo $response->getRedirectCount();
  36. // Iterate over each sent request and response
  37. foreach ($history->getAll() as $transaction) {
  38. // Request object
  39. echo $transaction['request']->getUrl() . "\n";
  40. // Response object
  41. echo $transaction['response']->getEffectiveUrl() . "\n";
  42. }
  43. // Or, simply cast the HistoryPlugin to a string to view each request and response
  44. echo $history;
  45. Disabling redirects
  46. -------------------
  47. You can disable redirects on a client by passing a configuration option in the client's constructor:
  48. .. code-block:: php
  49. $client = new Client(null, array('redirect.disable' => true));
  50. You can also disable redirects per request:
  51. .. code-block:: php
  52. $request = $client->get($url, array(), array('allow_redirects' => false));
  53. Redirects and non-repeatable streams
  54. ------------------------------------
  55. If you are redirected when sending data from a non-repeatable stream and some of the data has been read off of the
  56. stream, then you will get a ``Guzzle\Http\Exception\CouldNotRewindStreamException``. You can get around this error by
  57. adding a custom rewind method to the entity body object being sent in the request.
  58. .. code-block:: php
  59. $request = $client->post(
  60. 'http://httpbin.com/redirect/2',
  61. null,
  62. fopen('http://httpbin.com/get', 'r')
  63. );
  64. // Add a custom function that can be used to rewind the stream
  65. // (reopen in this example)
  66. $request->getBody()->setRewindFunction(function ($body) {
  67. $body->setStream(fopen('http://httpbin.com/get', 'r'));
  68. return true;
  69. );
  70. $response = $client->send();