title: PHP cURL to POST works, but not when I POST without cURL - What am I missing?
body: <p>I am in a situation where I cannot use PHP cURL module to POST my form data. I have found a great blog post showing how to POST without using cURL here:
  <a href="http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl" rel="nofollow noreferrer">HTTP POST from PHP, without cURL</a></p>
  
  <p>Here's my problem though. When I attempt to send my POST request, the request hits the (other) server but the content (POST data) is not transmitted. The other server does not get the content. HOWEVER, if I do use cURL, it works fine. What am I missing? How can I recreate the cURL HTTP POST request without using cURL?</p>
  
  <p>Here is the cURL code that works ($this->params is just $_POST which contains the form data):</p>
  
  <pre><code>$ch = curl_init($this-&gt;url);
  
  $params = http_build_query($this-&gt;params);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $params );
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
  curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
  curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
  
  $result = curl_exec($ch);
  curl_close($ch);
  
  return $result;
  </code></pre>
  
  <p>And here is my non-cURL version that does not work:</p>
  
  <pre><code>$postStr = http_build_query($this-&gt;params);
  
  $opts = array(
      'http'=&gt;array(
          'method' =&gt; $method,
          //'header' =&gt; 'Content-type: application/x-www-form-urlencoded',
          //'header' =&gt; 'content-type: multipart/form-data',
          'content-type' =&gt; 'application/x-www-form-urlencoded',
          'content-encoding' =&gt; 'UTF-8',
          'content' =&gt; $postStr
      )
  );
  
  $context = stream_context_create($opts);
  
  $result = file_get_contents($this-&gt;url, false, $context);
  
  return $result;
  </code></pre>
  
  <p>There are no errors or warnings. Just, the accepting server doesn't appear to the contents. Any ideas?</p>