本文介绍了CURL错误:Recv失败:连接由对等体复位 - PHP Curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我遇到这个奇怪的错误, CURL ERROR:Recv failed:Connection reset by peer 我没有连接到服务器,突然尝试通过CURL在PHP中连接到服务器我得到的错误。当我再次运行CURL脚本错误消失,然后工作良好的整个时间,如果我离开远程服务器空闲约30分钟或重新启动远程服务器,并尝试再次连接,我再次收到错误。所以看起来像连接是空闲的,然后所有的突然服务器唤醒,然后工作,然后再睡觉。 这是我的CURL脚本看起来。 p> $ url = Yii :: app() - > params ['pdfUrl']; $ body ='title ='。urlencode($ title)。& client_url ='。Yii :: app() - > params ['pdfClientURL']。& client_id = :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); 我得到以下: *关于连接()到196.41.139.168端口80(#0) *尝试196.xxx .. *连接 *连接到196.xxx )port 80(#0)> POST / serve /?r = pdf / generatePdf HTTP / 1.1 Host:196.xxx 接受:* / * Content-Length:7115 Content-Type:application / x-www-form-urlencoded 预期:100-继续 *恢复失败:连接由对等重置 *关闭连接#0 012 20:23: 49 GMT <服务器:Apache / 2.2.15(CentOS)< X-Powered-By:PHP / 5.3.3 <连接:close <传输编码:分块< Content-Type:text / html; charset = UTF-8 < *关闭连接#0 我已在下面添加toe删除默认标题仍然没有运气: curl_setopt($ c,CURLOPT_HTTPHEADER,array('Expect:')); >接受:* / * Content-Length:8414 Content-Type:> application / x-www-form-urlencoded > > * Recv failure:由对等连接重置> *关闭连接#0 r:Apache / 2.2.15(CentOS) X-Powered-By:PHP / 5.3.3<连接:传输编码: > Content-Type:text / html; charset = UTF-8< > *关闭连接#0 解决方案 简介 远程服务器向您发送了一个RST数据包,表示立即删除连接,而不是通常的握手。 可能的原因 A。 TCP / IP 这可能是TCP / IP问题,你需要解决与您的主机或升级您的操作系统大多数时间服务器在完成下载内容之前导致由对等连接重置 ..... B。 Kannel Bug 请注意,在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-防火墙1-INPUT -m状态--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并确保其未阻止您的连接 实验 尝试一个不同的服务器或远程服务器(如此多的云托管在线),并测试相同的脚本..如果它的工作,那么我猜测是真的... 您需要更新系统 其他代码相关 SSL 如果 Yii :: app() - > params ['pdfUrl'] 是一个包含 https 不包括正确SSL设置的网址也可能导致旧版本的curl中出现此错误 解决方法:确保OpenSSL已安装并启用,然后将其添加到您的代码中。 curl_setopt($ c,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ c,CURLOPT_SSL_VERIFYHOST,false); 我希望它有帮助 I'm having this strange error, CURL ERROR: Recv failure: Connection reset by peerThis is how it happens, if I did not connect to the server and all of a sudden trying to connect to the server via CURL in PHP I get the error. When I run the CURL script again the error disappears and then works well the whole time, if I leave the remote server idle for about 30mins or reboot the remote server and try to connect again, I get the error again. So it seems like the connection is idle and then all of sudden the server wakes up and then works and then sleeps again.This is how my CURL script looks.$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);I'm totally out of ideas and solutions, please help, I'll appreciate it!!!If I verbose the output to see what happens using curl_setopt ($c, CURLOPT_VERBOSE, TRUE);curl_setopt($c, CURLOPT_STDERR, $fp);I get the following* 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.1Host: 196.x.x.xAccept: */*Content-Length: 7115Content-Type: application/x-www-form-urlencodedExpect: 100-continue* Recv failure: Connection reset by peer* Closing connection #0012 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 #0I've added in the following toe remove the default header and still no luck: 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 解决方案 IntroductionThe remote server has sent you a RST packet, which indicates an immediate dropping of the connection, rather than the usual handshake. Possible CausesA. TCP/IPIt might be TCP/IP issue you need to resolve with your host or upgrade your OS most times connection is close before remote server before it finished downloading the content resulting to Connection reset by peer.....B. Kannel BugNote that there are some issues with TCP window scaling on some Linux kernels after v2.6.17. See the following bug reports for more information:https://bugs.launchpad.net/ubuntu/+source/linux-source-2.6.17/+bug/59331https://bugs.launchpad.net/ubuntu/+source/linux-source-2.6.20/+bug/89160C. PHP & CURL BugYou are using PHP/5.3.3 which has some serious bugs too ... i would advice you work with a more recent version of PHP and CURLhttps://bugs.php.net/bug.php?id=52828https://bugs.php.net/bug.php?id=52827https://bugs.php.net/bug.php?id=52202https://bugs.php.net/bug.php?id=50410D. Maximum Transmission UnitOne common cause of this error is that the MTU (Maximum Transmission Unit) size of packets travelling over your network connection have been changed from the default of 1500 bytes.If you have configured VPN this most likely must changed during configurationD. Firewall : iptablesIf you don't know your way around this guys they would cause some serious issues .. try and access the server you are connecting to check the followingYou have access to port 80 on that serverExample -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT`The Following is at the last line not before any other ACCEPTExample -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibitedCheck for ALL DROP , REJECT and make sure they are not blocking your connectionTemporary allow all connection as see if it foes through ExperimentTry a different server or remote server ( So many fee cloud hosting online) and test the same script .. if it works then i guesses are as good as true ... You need to update your system Others Code RelatedA. SSLIf Yii::app()->params['pdfUrl'] is a url with https not including proper SSL setting can also cause this error in old version of curlResolution : Make sure OpenSSL is installed and enabled then add this to your code curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);I hope it helps 这篇关于CURL错误:Recv失败:连接由对等体复位 - PHP Curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 23:59