1

我有以下设置:

$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);

但是当请求被发送到当前根本没有响应的服务器时,curl 在 1 秒内返回应答。我不明白为什么会这样,因为我已将 CURLOPT_CONNECTTIMEOUT 设置为 30,这应该意味着 curl 轮胎连接到服务器 30 秒。我需要 curl 尝试连接 30 秒,这不是 CURLOPT_CONNECTTIMEOUT 应该做的,还是有某种误解?

这是 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] => 
)
4

1 回答 1

1

在我看来,这意味着有一个连接被远程站点关闭(中止)。服务器拒绝了您的连接,因此无需等待任何东西。顺便说一句:connection_timeout 是一个设置,用于告诉 curl 等待多长时间,直到它停止尝试到达远程主机。– Mo.sch 13 小时前

于 2013-01-19T11:12:59.410 回答