gpt4 book ai didi

php - curl 错误 : Recv failure: Connection reset by peer - PHP Curl

转载 作者:IT老高 更新时间:2023-10-28 12:05:15 24 4
gpt4 key购买 nike

我遇到了这个奇怪的错误,CURL ERROR: Recv failure: Connection reset by peer

如果我没有连接到服务器并且突然尝试通过 PHP 中的 CURL 连接到服务器,则会发生这种情况,我会收到错误消息。当我再次运行 CURL 脚本时,错误消失,然后一直运行良好,如果我让远程服务器空闲大约 30 分钟或重新启动远程服务器并尝试再次连接,我再次收到错误。所以看起来连接是空闲的,然后服务器突然唤醒,然后工作,然后再次休眠。

这就是我的 CURL 脚本的外观。

$url = Yii::app()->params['pdfUrl'];
$body = 'title='.urlencode($title).'&client_url='.Yii::app()->params['pdfClientURL'].'&client_id='.Yii::app()->params['pdfClientID'].'&content='.urlencode(htmlentities($content));

$c = curl_init ($url);
$body = array(
"client_url"=>Yii::app()->params['pdfClientURL'],
"client_id"=>Yii::app()->params['pdfClientID'],
"title"=>urlencode($title),
"content"=>urlencode($content)

);
foreach($body as $key=>$value) { $body_str .= $key.'='.$value.'&'; }
rtrim($body_str,'&');

curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body_str);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($c, CURLOPT_CONNECTTIMEOUT , 0);
curl_setopt ($c, CURLOPT_TIMEOUT , 20);

$pdf = curl_exec ($c);
$errorCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
$curlInfo = curl_getinfo($c);
$curlError = curl_error($c);

curl_close ($c);

我完全没有想法和解决方案,请帮助,我将不胜感激!!!

如果我详细说明输出以查看使用

会发生什么
curl_setopt ($c, CURLOPT_VERBOSE, TRUE);
curl_setopt($c, CURLOPT_STDERR, $fp);

我得到以下信息

* About to connect() to 196.41.139.168 port 80 (#0)
* Trying 196.x.x.x... * connected
* Connected to 196.x.x.x (196.x.x.x) port 80 (#0)
> POST /serve/?r=pdf/generatePdf HTTP/1.1
Host: 196.x.x.x
Accept: */*
Content-Length: 7115
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

* Recv failure: Connection reset by peer
* Closing connection #0
012 20:23:49 GMT
< Server: Apache/2.2.15 (CentOS)
< X-Powered-By: PHP/5.3.3
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
<
* Closing connection #0

我在下面的脚趾中添加了删除默认标题,但仍然没有运气:

curl_setopt ($c, CURLOPT_HTTPHEADER, array( 'Expect:' ) );

> Accept: */* Content-Length: 8414 Content-Type:
> application/x-www-form-urlencoded
>
> * Recv failure: Connection reset by peer
> * Closing connection #0 r: Apache/2.2.15 (CentOS) < X-Powered-By: PHP/5.3.3 < Connection: close < Transfer-Encoding: chunked <
> Content-Type: text/html; charset=UTF-8 <
> * Closing connection #0

最佳答案

简介

远程服务器已向您发送了一个 RST 数据包,表示立即断开连接,而不是通常的握手。

可能的原因

A. TCP/IP

这可能是您需要通过主机解决的 TCP/IP 问题或升级您的操作系统,大多数时候远程服务器在完成下载内容之前关闭了与远程服务器的连接,导致 Connection reset by peer。 ....

B. 内核错误

请注意,在 v2.6.17 之后,某些 Linux 内核上的 TCP 窗口缩放存在一些问题。有关详细信息,请参阅以下错误报告:

https://bugs.launchpad.net/ubuntu/+source/linux-source-2.6.17/+bug/59331

https://bugs.launchpad.net/ubuntu/+source/linux-source-2.6.20/+bug/89160

C. PHP 和 CURL 错误

您正在使用 PHP/5.3.3,它也有一些严重的错误...我建议您使用更新版本的 PHP curl

https://bugs.php.net/bug.php?id=52828

https://bugs.php.net/bug.php?id=52827

https://bugs.php.net/bug.php?id=52202

https://bugs.php.net/bug.php?id=50410

D. 最大传输单位

此错误的一个常见原因是通过网络连接传输的数据包的 MTU(最大传输单元)大小已从默认的 1500 字节更改。如果您配置了 VPN,这很可能必须在配置期间进行更改

D. 防火墙:iptables

如果您不了解这些人,他们可能会导致一些严重的问题.. 尝试访问您正在连接的服务器以检查以下内容

  • 您可以访问该服务器上的 80 端口

例子

 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT`
  • 以下内容位于最后一行,不在任何其他 ACCEPT 之前

例子

  -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited 
  • 检查所有 DROP、REJECT 并确保它们没有阻止您的连接

  • 暂时允许所有连接,看看它是否通过

实验

在不同的服务器或远程服务器上尝试(网上有很多免费的云托管)并测试相同的脚本。如果它有效,那么我的猜测是正确的...... 你需要更新你的系统

其他代码相关

A. SSL

如果 Yii::app()->params['pdfUrl'] 是带有 https 的 url 不包括正确的 SSL 设置也可能在旧版本中导致此错误 curl 的

解决方案:确保已安装并启用 OpenSSL,然后将其添加到您的代码中

curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);

关于php - curl 错误 : Recv failure: Connection reset by peer - PHP Curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com