faq.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ===
  2. FAQ
  3. ===
  4. Does Guzzle require cURL?
  5. =========================
  6. No. Guzzle can use any HTTP handler to send requests. This means that Guzzle
  7. can be used with cURL, PHP's stream wrapper, sockets, and non-blocking libraries
  8. like `React <http://reactphp.org/>`_. You just need to configure an HTTP handler
  9. to use a different method of sending requests.
  10. .. note::
  11. Guzzle has historically only utilized cURL to send HTTP requests. cURL is
  12. an amazing HTTP client (arguably the best), and Guzzle will continue to use
  13. it by default when it is available. It is rare, but some developers don't
  14. have cURL installed on their systems or run into version specific issues.
  15. By allowing swappable HTTP handlers, Guzzle is now much more customizable
  16. and able to adapt to fit the needs of more developers.
  17. Can Guzzle send asynchronous requests?
  18. ======================================
  19. Yes. You can use the ``requestAsync``, ``sendAsync``, ``getAsync``,
  20. ``headAsync``, ``putAsync``, ``postAsync``, ``deleteAsync``, and ``patchAsync``
  21. methods of a client to send an asynchronous request. The client will return a
  22. ``GuzzleHttp\Promise\PromiseInterface`` object. You can chain ``then``
  23. functions off of the promise.
  24. .. code-block:: php
  25. $promise = $client->requestAsync('GET', 'http://httpbin.org/get');
  26. $promise->then(function ($response) {
  27. echo 'Got a response! ' . $response->getStatusCode();
  28. });
  29. You can force an asynchronous response to complete using the ``wait()`` method
  30. of the returned promise.
  31. .. code-block:: php
  32. $promise = $client->requestAsync('GET', 'http://httpbin.org/get');
  33. $response = $promise->wait();
  34. How can I add custom cURL options?
  35. ==================================
  36. cURL offers a huge number of `customizable options <http://us1.php.net/curl_setopt>`_.
  37. While Guzzle normalizes many of these options across different handlers, there
  38. are times when you need to set custom cURL options. This can be accomplished
  39. by passing an associative array of cURL settings in the **curl** key of a
  40. request.
  41. For example, let's say you need to customize the outgoing network interface
  42. used with a client.
  43. .. code-block:: php
  44. $client->request('GET', '/', [
  45. 'curl' => [
  46. CURLOPT_INTERFACE => 'xxx.xxx.xxx.xxx'
  47. ]
  48. ]);
  49. How can I add custom stream context options?
  50. ============================================
  51. You can pass custom `stream context options <http://www.php.net/manual/en/context.php>`_
  52. using the **stream_context** key of the request option. The **stream_context**
  53. array is an associative array where each key is a PHP transport, and each value
  54. is an associative array of transport options.
  55. For example, let's say you need to customize the outgoing network interface
  56. used with a client and allow self-signed certificates.
  57. .. code-block:: php
  58. $client->request('GET', '/', [
  59. 'stream' => true,
  60. 'stream_context' => [
  61. 'ssl' => [
  62. 'allow_self_signed' => true
  63. ],
  64. 'socket' => [
  65. 'bindto' => 'xxx.xxx.xxx.xxx'
  66. ]
  67. ]
  68. ]);
  69. Why am I getting an SSL verification error?
  70. ===========================================
  71. You need to specify the path on disk to the CA bundle used by Guzzle for
  72. verifying the peer certificate. See :ref:`verify-option`.
  73. What is this Maximum function nesting error?
  74. ============================================
  75. Maximum function nesting level of '100' reached, aborting
  76. You could run into this error if you have the XDebug extension installed and
  77. you execute a lot of requests in callbacks. This error message comes
  78. specifically from the XDebug extension. PHP itself does not have a function
  79. nesting limit. Change this setting in your php.ini to increase the limit::
  80. xdebug.max_nesting_level = 1000
  81. Why am I getting a 417 error response?
  82. ======================================
  83. This can occur for a number of reasons, but if you are sending PUT, POST, or
  84. PATCH requests with an ``Expect: 100-Continue`` header, a server that does not
  85. support this header will return a 417 response. You can work around this by
  86. setting the ``expect`` request option to ``false``:
  87. .. code-block:: php
  88. $client = new GuzzleHttp\Client();
  89. // Disable the expect header on a single request
  90. $response = $client->request('PUT', '/', ['expect' => false]);
  91. // Disable the expect header on all client requests
  92. $client = new GuzzleHttp\Client(['expect' => false]);
  93. How can I track redirected requests?
  94. ====================================
  95. You can enable tracking of redirected URIs and status codes via the
  96. `track_redirects` option. Each redirected URI and status code will be stored in the
  97. ``X-Guzzle-Redirect-History`` and the ``X-Guzzle-Redirect-Status-History``
  98. header respectively.
  99. The initial request's URI and the final status code will be excluded from the results.
  100. With this in mind you should be able to easily track a request's full redirect path.
  101. For example, let's say you need to track redirects and provide both results
  102. together in a single report:
  103. .. code-block:: php
  104. // First you configure Guzzle with redirect tracking and make a request
  105. $client = new Client([
  106. RequestOptions::ALLOW_REDIRECTS => [
  107. 'max' => 10, // allow at most 10 redirects.
  108. 'strict' => true, // use "strict" RFC compliant redirects.
  109. 'referer' => true, // add a Referer header
  110. 'track_redirects' => true,
  111. ],
  112. ]);
  113. $initialRequest = '/redirect/3'; // Store the request URI for later use
  114. $response = $client->request('GET', $initialRequest); // Make your request
  115. // Retrieve both Redirect History headers
  116. $redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History')[0]; // retrieve Redirect URI history
  117. $redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History')[0]; // retrieve Redirect HTTP Status history
  118. // Add the initial URI requested to the (beginning of) URI history
  119. array_unshift($redirectUriHistory, $initialRequest);
  120. // Add the final HTTP status code to the end of HTTP response history
  121. array_push($redirectCodeHistory, $response->getStatusCode());
  122. // (Optional) Combine the items of each array into a single result set
  123. $fullRedirectReport = [];
  124. foreach ($redirectUriHistory as $key => $value) {
  125. $fullRedirectReport[$key] = ['location' => $value, 'code' => $redirectCodeHistory[$key]];
  126. }
  127. echo json_encode($fullRedirectReport);