简体   繁体   English

PHP curl CURLOPT_CONNECTTIMEOUT对连接超时没有影响

[英]PHP curl CURLOPT_CONNECTTIMEOUT has no effect on connection timeout

I have following settings: 我有以下设置:

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_TIMEOUT, 30);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);

But when request is sent to server, which is currently not responding at all, curl returns answer in 1 second. 但是,当请求发送到当前完全没有响应的服务器时,curl将在1秒内返回答案。 I don't understand why that is happening, because I have set CURLOPT_CONNECTTIMEOUT to 30, which should mean that curl tires to connect to server 30 seconds. 我不明白为什么会这样,因为我将CURLOPT_CONNECTTIMEOUT设置为30,这应该意味着curl弯轮胎要在30秒内连接到服务器。 I need curl to try connection for 30 seconds, isn't that what CURLOPT_CONNECTTIMEOUT is supposed to do, or is there some kind of misunderstanding? 我需要卷曲才能尝试连接30秒钟,这不是CURLOPT_CONNECTTIMEOUT应该执行的操作,还是存在某种误解?

Here is what print_r(curl_getinfo($handle), true) returns: 这是print_r(curl_getinfo($ handle),true)返回的内容:

(
    [url] => http://server.url
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.045
    [namelookup_time] => 0.016
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 
)

In my opinion this means, that there was a connection which got closed by the remote site (aborted). 在我看来,这意味着某个连接已被远程站点关闭(终止)。 The server denied your connection so there is no need to wait for anything. 服务器拒绝了您的连接,因此无需等待任何事情。 BTW: connection_timeout is a setting to tell curl how long to wait until it stops trying to reach the remote host. 顺便说一句:connection_timeout是一项设置,用于告诉curl等待停止到达远程主机的等待时间。 – Mo.sch 13 hours ago – Mo.sch 13小时前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM