title: CURL ERROR: Recv failure: Connection reset by peer - PHP Curl
body: <p>I'm having this strange error, <strong>CURL ERROR: Recv failure: Connection reset by peer</strong></p>
  
  <p>This 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.</p>
  
  <p>This is how my CURL script looks.</p>
  
  <pre><code>$url = Yii::app()-&gt;params['pdfUrl'];
              $body = 'title='.urlencode($title).'&amp;client_url='.Yii::app()-&gt;params['pdfClientURL'].'&amp;client_id='.Yii::app()-&gt;params['pdfClientID'].'&amp;content='.urlencode(htmlentities($content));
  
              $c = curl_init ($url);
              $body = array(
                  "client_url"=&gt;Yii::app()-&gt;params['pdfClientURL'],
                  "client_id"=&gt;Yii::app()-&gt;params['pdfClientID'],
                  "title"=&gt;urlencode($title),
                  "content"=&gt;urlencode($content)
  
              );
              foreach($body as $key=&gt;$value) { $body_str .= $key.'='.$value.'&amp;'; }
                  rtrim($body_str,'&amp;');
  
              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);
  </code></pre>
  
  <p>I'm totally out of ideas and solutions, please help, I'll appreciate it!!!</p>
  
  <p>If I verbose the output to see what happens using </p>
  
  <pre><code>curl_setopt ($c, CURLOPT_VERBOSE, TRUE);
  curl_setopt($c, CURLOPT_STDERR, $fp); 
  </code></pre>
  
  <p>I get the following</p>
  
  <pre><code>* 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)
  &gt; 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
  &lt; Server: Apache/2.2.15 (CentOS)
  &lt; X-Powered-By: PHP/5.3.3
  &lt; Connection: close
  &lt; Transfer-Encoding: chunked
  &lt; Content-Type: text/html; charset=UTF-8
  &lt; 
  * Closing connection #0
  </code></pre>
  
  <p>I've added in the following toe remove the default header and still no luck: </p>
  
  <pre><code>curl_setopt ($c, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
  
  &gt; Accept: */* Content-Length: 8414 Content-Type:
  &gt; application/x-www-form-urlencoded
  &gt; 
  &gt; * Recv failure: Connection reset by peer
  &gt; * Closing connection #0 r: Apache/2.2.15 (CentOS) &lt; X-Powered-By: PHP/5.3.3 &lt; Connection: close &lt; Transfer-Encoding: chunked &lt;
  &gt; Content-Type: text/html; charset=UTF-8 &lt; 
  &gt; * Closing connection #0
  </code></pre>