【问题标题】:Php Curl :on timeout, close connection but not return any contentPhp Curl:超时,关闭连接但不返回任何内容
【发布时间】: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。

标签: php php-curl


【解决方案1】:

它是二进制传输的内容.. 但它就像一个流内容。 例如,如果没有 CURLOPT_TIMEOUT curl 进入无限状态并且连接不会停止,但是如果激活超时,连接会终止但不会传输读取的输出。 如果我使用 file_get_contents,则在超时时,我有带或不带超时设置的输出。 为什么需要使用 curl 而不是 file_get_contents ?因为我需要用多卷曲设置卷曲。使用 file_get_contents 我无法在不等待的情况下拨打多个电话。

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 2012-02-15
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2013-12-07
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多