Search code examples
phpcurltimeoutconnection-timeout

Curl force to Timeout


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.


Solution

  • 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.