CURL ERROR: Failure Recv: Connection reset by peer - PHP Curl

I got this weird error, CURL ERROR: Error Recv: connection reset using peer

This is how it happens, if I did not connect to the server and suddenly try to connect to the server via CURL in PHP, I get an error. When I run the CURL script again, the error disappears and then works well all the time if I leave on the remote server for about 30 minutes or reboot the remote server and try to connect again, I will get the error again. So it looks like the connection is idle and then suddenly the server wakes up and then works and then sleeps again.

This is what my CURL script looks like.

$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 have a lot of ideas and solutions, please help, I will be grateful!

If I complete the output to see what happens with

 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.xxx.. * connected * Connected to 196.xxx (196.xxx) port 80 (#0) > POST /serve/?r=pdf/generatePdf HTTP/1.1 Host: 196.xxx 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 

I added the following header to 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 
+45
php curl yii
Apr 23 2018-12-12T00:
source share
5 answers

Introduction

The remote server sent you an RST packet, which indicates an immediate disconnection, not a normal handshake.

Possible reasons

but. TCP / IP

This may be a TCP / IP problem that you need to resolve from your host or upgrade your OS in most cases. The connection is closed before the remote server before it finishes loading the contents as a result of Connection reset by peer .....

B. Error Kannel

Note that there are some problems with scaling the TCP window on some Linux kernels after version 2.6.17. See the following error reports for more information:

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. Error PHP and CURL

You are using PHP/5.3.3 , which also has serious errors ... I would advise you to work with a newer version of PHP and 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. Maximum transmission unit

One of the common causes of this error is that the size of the MTU (Maximum Transmission Unit) of packets moving over your network connection has been changed from 1500 bytes by default. If you have configured a VPN , this is likely to change during configuration.

D. Firewall: iptables

If you don’t know your way around these guys, they can cause some serious problems. Try and connect to the server you are connecting to to check the following

  • You have access to port 80 on this server.

Example

  -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT` 
  • Below is the last line not earlier than any other ACCEPT

Example

  -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited 
  • Check ALL DROP, REJECT and make sure that they do not block your connection.

  • Temporarily allow all connections if they see it spinning through

Experiment

Try using a different server or a remote server (there are so many paid cloud hosting sites on the Internet) and test the same script .. if it works, then I can guess as good as the truth ... You need to update your system

Other related code

but. SSL

If Yii::app()->params['pdfUrl'] is a URL with https , apart from the proper SSL setting, it can also cause this error in the old version of curl

Resolution: make sure OpenSSL is installed and enabled, then add it to your code

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

I hope this helps

+79
Apr 27 '12 at 11:35
source share

Usually this error means that the connection was made with the server, but this connection was closed by the remote server. This may be due to a slow server, a problem with the remote server, a network problem, or (possibly) some kind of security error when sending data to the remote server, but I find this unlikely.

Usually a network error will itself be resolved taking into account some time, but it looks like you have already given it a little time.

cURL sometimes occurs with SSL and SSL certificates. I think your Apache and / or PHP were compiled with the latest cURL and cURL SSL libraries, and I don’t think that OpenSSL was installed on your web server.

Although I cannot be sure, however, I believe that cURL has historically been mistaken for SSL certificates, whereas Open SSL does not.

In any case, try installing Open SSL on the server and try again, and this should help you get rid of this error.

+9
Apr 30 '12 at 18:00
source share

So what is the URL that Yii::app()->params['pdfUrl'] ? You say that it should be https, but the log shows that it connects to port 80 ... which is almost not configured by the server to accept https connections. cURL is smart enough to know that https should be on port 443 ... which would suggest that your URL has something awkward: https://196.41.139.168:80/serve/?r=pdf/generatePdf

This will terminate the connection when Apache on the other end cannot exchange https with you on this port.

Do you realize that the first definition of $body is replaced when you set $body to an array two lines later? {This is probably just an artifact that you are trying to solve the problem.} You also do not encode the values ​​of client_url and client_id (the first, possibly containing characters that need to be escaped!) Oh, and you add to $body_str without initialization.

From your verbose output, we see that cURL adds a content-length header, but ... is this correct? I see that some comments in the intervals of this number are erroneous (especially with older versions) ... if this number was small (for example), you would get a reset connection before all data is sent. You can manually insert the header:

 curl_setopt ($c, CURLOPT_HTTPHEADER, array("Content-Length: ". strlen($body_str))); 

Oh, and there is a handy http_build_query function that converts an array of name / value pairs into a URL encoded string for you.

All this collapses into the final code:

 $post=http_build_query(array( "client_url"=>Yii::app()->params['pdfClientURL'], "client_id"=>Yii::app()->params['pdfClientID'], "title"=>$title, "content"=>$content)); //Open to URL $c=curl_init(Yii::app()->params['pdfUrl']); //Send post curl_setopt ($c, CURLOPT_POST, true); //Optional: [try with/without] curl_setopt ($c, CURLOPT_HTTPHEADER, array("Content-Length: ".strlen($post))); curl_setopt ($c, CURLOPT_POSTFIELDS, $post); curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); curl_setopt ($c, CURLOPT_CONNECTTIMEOUT , 0); curl_setopt ($c, CURLOPT_TIMEOUT , 20); //Collect result $pdf = curl_exec ($c); $curlInfo = curl_getinfo($c); curl_close($c); 
+3
Apr 27 '12 at 17:31
source share

This is a problem with the firewall, if you are using the VMware application, make sure that the firewall on the antivirus is turned off or allows connections.

If this server is on a secure network, read the server firewall rules.

Thanks Ganesh PNS

+1
Oct 13 '15 at 5:30
source share

In my case, the problem was in the URL. I use https://example.com - but they guarantee 'www.' - so when I switched to https://www.example.com everything was fine. The correct header was sent to "Host: www.example.com".

You can try to make a request in firefox brwoser, save it and copy it as cURL - the way I found it.

0
Feb 04 '16 at 20:59
source share



All Articles