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, there are times when you need to set custom cURL options. This can be accomplished by passing an associative array of cURL settings in the curl key of a request.

For example, let’s say you need to customize the outgoing network interface used with a client.

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

If you use asynchronous requests with cURL multi handler and want to tweak it, additional options can be specified as an associative array in the options key of the CurlMultiHandler constructor.

  1. use \GuzzleHttp\Client;
  2. use \GuzzleHttp\HandlerStack;
  3. use \GuzzleHttp\Handler\CurlMultiHandler;
  4. $client = new Client(['handler' => HandlerStack::create(new CurlMultiHandler([
  5. 'options' => [
  6. CURLMOPT_MAX_TOTAL_CONNECTIONS => 50,
  7. CURLMOPT_MAX_HOST_CONNECTIONS => 5,
  8. ]
  9. ]))]);