-1

I am trying to test my timeout conditions using curl and force the website to timeout. Here is my curl settings:

curl_setopt_array($curl, array(
 CURLOPT_URL => "https://app.sample.com/api/abc/changelogs?last=3",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_CONNECTTIMEOUT => 0,
 CURLOPT_TIMEOUT => 0,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "GET",
 CURLOPT_HTTPHEADER => array(
 "cache-control: no-cache"
 ),
));

Eventhough I changed the CONNECTTIMEOUT and TIMEOUT into 0 / 0.000001 it still wont time out. Any help would be nice.

Squish
  • 419
  • 4
  • 22
  • No wonder, since `CURLOPT_CONNECTTIMEOUT` is an `int`... – Kevin Kopf Mar 05 '18 at 12:09
  • [Documentation](http://www.php.net/manual/en/function.curl-setopt.php) explains that 0 is like no timeout, and @AlexKarshin explained that it's an integer. So try to set it to 1. – Anthony Mar 05 '18 at 12:10
  • @AlexKarshin Then is there a way to force the time out while using an int since I have to start from 1. – Squish Mar 05 '18 at 12:14
  • @AnthonyB if i set it to 1, it wont trigger a timeout – Squish Mar 05 '18 at 12:15
  • @SnowBut It's because this value is an integer in seconds. But Alex's anwser explains that you can use an other value in milliseconds. – Anthony Mar 05 '18 at 12:16

1 Answers1

2

As per the documentation, CURLOPT_CONNECTTIMEOUT is an integer:

The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

If you have cURL >= 7.16.2 and PHP >= 5.2.3, you may use CURLOPT_CONNECTTIMEOUT_MS:

The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

Though, you should not confuse it with CURLOPT_TIMEOUT and CURLOPT_TIMEOUT_MS:

CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute. CURLOPT_TIMEOUT_MS - The maximum number of milliseconds to allow cURL functions to execute. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

The obvious difference being that CURLOPT_CONNECTTIMEOUT is timeout before script dies if no connection is present; whilst CURLOPT_TIMEOUT kills the script after defined number of seconds.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • Great! A code sample directly reproducible would be great. – Anthony Mar 05 '18 at 12:17
  • @AnthonyB I'm not sure... Should I take OP's code and simply change `CURLOPT_CONNECTTIMEOUT => 0,` to `CURLOPT_CONNECTTIMEOUT => 1,`? Feels degrading to me. Surely OP can do it himself. – Kevin Kopf Mar 05 '18 at 12:22
  • I finally understand, thank you for you help @AnthonyB – Squish Mar 05 '18 at 12:23
  • @AlexKarshin Thank you for you help :) – Squish Mar 05 '18 at 12:29
  • @AlexKarshin In [how to answer](https://stackoverflow.com/help/how-to-answer) is it said `but it should also include “try this instead"` so even if it is a little example it could be useful. According to me a little example using the right option could be good, but is not mandatory. – Anthony Mar 05 '18 at 12:43