Php Curl:超时时,关闭连接但不返回任何内容

时间:2019-06-05 21:46:45

标签: php php-curl

当我对服务器运行curl并设置超时时,服务器无法传输“ Content-Length”标头设置的所有内容。问题是连接由于超时而断开,没有任何输出。 选项中是否有运算符,即使连接超时也可以将内容传输到输出?

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_VERBOSE => true,
    CURLOPT_URL => $url,
    CURLOPT_TIMEOUT =>1800,
));
$data= curl_exec($curl);
curl_close($curl);

echo $data;
?>

* About to connect() to 2.100.x.x port 80 (#0)
*   Trying 2.100.x.x... * connected
* Connected to 2.100.x.x (2.100.x.x) port 80 (#0)
> GET /example.html HTTP/1.1
Host: 2.100.x.x
Accept: */*

< HTTP/1.1 200 OK
< Date: Wed, 05 Jun 2019 21:00:36 GMT
< Content-Type: text/html
< Content-Length: 16781312
< Cache-Control: private
< Connection: close
<
* Operation timed out after 1800000 milliseconds with 4598060 out of 16781312 bytes received
* Closing connection #0

1 个答案:

答案 0 :(得分:0)

它是二进制传输的内容。但是它就像流媒体内容一样。 例如,如果没有CURLOPT_TIMEOUT,curl会无限跳转,并且连接不会停止,但是如果激活超时,连接会终止但不传输读取的输出,则会出现问题。 如果我使用file_get_contents,则在超时时我可以选择是否设置超时。 为什么需要使用curl而不是file_get_contents?因为我需要设置多卷曲的卷曲。使用file_get_contents时,我不能不进行多次调用。