FAQ

Does Guzzle require cURL?

No. Guzzle can use any HTTP handler to send requests. This means that Guzzlecan be used with cURL, PHP's stream wrapper, sockets, and non-blocking librarieslike React. You just need to configure an HTTP handlerto use a different method of sending requests.

Note

Guzzle has historically only utilized cURL to send HTTP requests. cURL isan amazing HTTP client (arguably the best), and Guzzle will continue to useit by default when it is available. It is rare, but some developers don'thave cURL installed on their systems or run into version specific issues.By allowing swappable HTTP handlers, Guzzle is now much more customizableand able to adapt to fit the needs of more developers.

Can Guzzle send asynchronous requests?

Yes. You can use the requestAsync, sendAsync, getAsync,headAsync, putAsync, postAsync, deleteAsync, and patchAsyncmethods of a client to send an asynchronous request. The client will return aGuzzleHttp\Promise\PromiseInterface object. You can chain thenfunctions off of the promise.

  1. $promise = $client->requestAsync('GET', 'http://httpbin.org/get');
  2. $promise->then(function ($response) {
  3. echo 'Got a response! ' . $response->getStatusCode();
  4. });

You can force an asynchronous response to complete using the wait() methodof the returned promise.

  1. $promise = $client->requestAsync('GET', 'http://httpbin.org/get');
  2. $response = $promise->wait();

How can I add custom cURL options?

cURL offers a huge number of customizable options.While Guzzle normalizes many of these options across different handlers, thereare times when you need to set custom cURL options. This can be accomplishedby passing an associative array of cURL settings in the curl key of arequest.

For example, let's say you need to customize the outgoing network interfaceused with a client.

  1. $client->request('GET', '/', [
  2. 'curl' => [
  3. CURLOPT_INTERFACE => 'xxx.xxx.xxx.xxx'
  4. ]
  5. ]);

How can I add custom stream context options?

You can pass custom stream context optionsusing the stream_context key of the request option. The stream_contextarray is an associative array where each key is a PHP transport, and each valueis an associative array of transport options.

For example, let's say you need to customize the outgoing network interfaceused with a client and allow self-signed certificates.

  1. $client->request('GET', '/', [
  2. 'stream' => true,
  3. 'stream_context' => [
  4. 'ssl' => [
  5. 'allow_self_signed' => true
  6. ],
  7. 'socket' => [
  8. 'bindto' => 'xxx.xxx.xxx.xxx'
  9. ]
  10. ]
  11. ]);

Why am I getting an SSL verification error?

You need to specify the path on disk to the CA bundle used by Guzzle forverifying the peer certificate. See verify.

What is this Maximum function nesting error?

Maximum function nesting level of '100' reached, aborting

You could run into this error if you have the XDebug extension installed andyou execute a lot of requests in callbacks. This error message comesspecifically from the XDebug extension. PHP itself does not have a functionnesting limit. Change this setting in your php.ini to increase the limit:

  1. xdebug.max_nesting_level = 1000

Why am I getting a 417 error response?

This can occur for a number of reasons, but if you are sending PUT, POST, orPATCH requests with an Expect: 100-Continue header, a server that does notsupport this header will return a 417 response. You can work around this bysetting the expect request option to false:

  1. $client = new GuzzleHttp\Client();
  2.  
  3. // Disable the expect header on a single request
  4. $response = $client->request('PUT', '/', ['expect' => false]);
  5.  
  6. // Disable the expect header on all client requests
  7. $client = new GuzzleHttp\Client(['expect' => false]);

How can I track redirected requests?

You can enable tracking of redirected URIs and status codes via thetrack_redirects option. Each redirected URI and status code will be stored in theX-Guzzle-Redirect-History and the X-Guzzle-Redirect-Status-Historyheader respectively.

The initial request's URI and the final status code will be excluded from the results.With this in mind you should be able to easily track a request's full redirect path.

For example, let's say you need to track redirects and provide both resultstogether in a single report:

  1. // First you configure Guzzle with redirect tracking and make a request
  2. $client = new Client([
  3. RequestOptions::ALLOW_REDIRECTS => [
  4. 'max' => 10, // allow at most 10 redirects.
  5. 'strict' => true, // use "strict" RFC compliant redirects.
  6. 'referer' => true, // add a Referer header
  7. 'track_redirects' => true,
  8. ],
  9. ]);
  10. $initialRequest = '/redirect/3'; // Store the request URI for later use
  11. $response = $client->request('GET', $initialRequest); // Make your request
  12.  
  13. // Retrieve both Redirect History headers
  14. $redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History')[0]; // retrieve Redirect URI history
  15. $redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History')[0]; // retrieve Redirect HTTP Status history
  16.  
  17. // Add the initial URI requested to the (beginning of) URI history
  18. array_unshift($redirectUriHistory, $initialRequest);
  19.  
  20. // Add the final HTTP status code to the end of HTTP response history
  21. array_push($redirectCodeHistory, $response->getStatusCode());
  22.  
  23. // (Optional) Combine the items of each array into a single result set
  24. $fullRedirectReport = [];
  25. foreach ($redirectUriHistory as $key => $value) {
  26. $fullRedirectReport[$key] = ['location' => $value, 'code' => $redirectCodeHistory[$key]];
  27. }
  28. echo json_encode($fullRedirectReport);