title: PHP cURL through proxy
body: <p>I'm trying to send a cURL request through a proxy in PHP.</p>
  
  <pre><code>$c = curl_init();
  curl_setopt($c, CURLOPT_URL, $this-&gt;url);
  curl_setopt($c, CURLOPT_PROXY, $this-&gt;proxy);
  curl_setopt($c, CURLOPT_POST, true);
  curl_setopt($c, CURLOPT_USERAGENT, $this-&gt;browser);
  curl_setopt($c, CURLOPT_POSTFIELDS, $requestData);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($c, CURLOPT_HEADER, 1);
  curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
  $result = curl_exec($c);
  </code></pre>
  
  <p>And here are the errors I get:</p>
  
  <blockquote>
    <p>HTTP_CODE: 201 (ID: 511a59debf97b)<br>
    HTTP_CODE: 201 (ID: 511a59e25bc89)<br>
    HTTP_CODE: 201 (ID: 511a59e47797c)<br>
    HTTP_CODE: 201 (ID: 511a59e641246)<br>
    HTTP_CODE: 400 (ID: 511a59e9850dc)<br>
    HTTP_CODE: 400 (ID: 511a59ea59bda)<br>
    HTTP_CODE: 400 (ID: 511a59eb90572)<br>
    HTTP_CODE: 201 (ID: 511a59ec647ad)<br>
    HTTP_CODE: 411 (ID: 511a59efda30a)<br>
    HTTP_CODE: 201 (ID: 511a59f0c6c70)  </p>
  </blockquote>
  
  <p>As you can see, they're all 2xx and 4xx and I've no idea why this is so. I should probably also mention that sometimes (about 1/8 of all requests) the requests "work", so it fulfils the purpose of this script but it still logs this kind of errors.  </p>
  
  <p>Here's the error log code</p>
  
  <pre><code>#check for errors
  $errorsFile = $this-&gt;folders["root"].$this-&gt;folders["db"].$this-&gt;files["errors"];
  if(curl_errno($c)) file_put_contents($errorsFile, "CURL_ERRNO: ".curl_errno($c)." (ID: ".$this-&gt;requestID.")\n", FILE_APPEND);
  else {
      $httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE); #checks http status
      if($httpCode != 200) file_put_contents($errorsFile, "HTTP_CODE: ".$httpCode." (ID: ".$this-&gt;requestID.")\n", FILE_APPEND);
  }
  curl_close($c);
  #log
  $log = str_replace("\n", "", date("d.m.Y - H:i", time()) . " " . $this-&gt;proxy . " [" . $this-&gt;browser . "] (ID: ".$this-&gt;requestID.")")."\n" ;
  file_put_contents($this-&gt;folders["root"].$this-&gt;folders["db"].$this-&gt;files["logs"], $log, FILE_APPEND);
  </code></pre>
  
  <p>As you can see, I also log cURL errors (if they happen) but they rarely happen.</p>
  
  <p>However, I'm not sure where these errors come from and why it works sometimes and sometimes it doesn't. If there's something wrong with the proxy, I should get a cURL error not a HTTP error, right? So what's wrong?</p>
  
  <p><strong>UPDATE</strong>
  Thanks to @Dharmavir</p>
  
  <blockquote>
    <p>proxy seems to be a problem here. Can you add a header in cURL request
     with "Content-Length: ". I have experience in
     past that sometimes proxy or firewall (ipcop in my case) blocked
     requests w/o content length headers and considered as virus or bad
     requests.</p>
  </blockquote>
  
  <p>I didn't mention that <code>$proxy</code> is randomly chosen and therefore always a different one. I tested manually a proxy that worked and it handled all 10 of 10 requests successfully. </p>