newbie
newbie

Reputation: 24645

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

Here is what print_r(curl_getinfo($handle), true) returns:

(
    [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] => 
)

Upvotes: 1

Views: 7796

Answers (1)

mosch
mosch

Reputation: 1035

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. – Mo.sch 13 hours ago

Upvotes: 1

Related Questions