【发布时间】:2019-06-05 21:46:45
【问题描述】:
当我对服务器运行 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
【问题讨论】:
-
"将内容传输到输出" ...什么内容,到什么输出?你说的是
echo $data行吗?很明显,如果 cURL 请求超时,远程服务器将没有响应,所以什么也没有显示。你不能凭空做出回应。您可以显示自己的错误消息 -
话虽如此,如果您遇到超时问题,您是否考虑过增加超时值的长度?
-
也许你不应该设置超时,而是设置 CURLOPT_CONNECTTIMEOUT。