卷曲力超时

Curl force to Timeout

我正在尝试使用 curl 测试我的超时条件并强制网站超时。这是我的卷曲设置:

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"
 ),
));

尽管我将 CONNECTTIMEOUT 和 TIMEOUT 更改为 0 / 0.000001,它仍然不会超时。任何帮助都会很好。

根据the documentationCURLOPT_CONNECTTIMEOUT是一个整数:

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

如果您的 cURL >= 7.16.2 且 PHP >= 5.2.3,您可以使用 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.

不过,您不应该将它与 CURLOPT_TIMEOUTCURLOPT_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.

明显的区别是 CURLOPT_CONNECTTIMEOUT 是在没有连接的情况下脚本终止之前的超时;而 CURLOPT_TIMEOUT 在定义的秒数后终止脚本。