title: PHP stream_context_create and HTTP headers - array or string, \r\n at the end, Content-Length optional?
body: <p>I am troubleshooting some problems with POSTing to a remote site, specifically the remote host never returns any data (empty string).</p>
  
  <p>Before I try to troubleshoot anything else, I want to make sure the calling code is actually correct. The code is:</p>
  
  <pre><code>$context = stream_context_create(array('http' =&gt; array(
      'method'        =&gt; "POST",
      'header'        =&gt; "Content-Type: application/xml",
      'timeout'       =&gt; 60.0,
      'ignore_errors' =&gt; true, # return body even if HTTP status != 200
      'content'       =&gt; $send_xml
  )));
  
  $response = trim(file_get_contents($this-&gt;bulk_service_url, false, $context));
  </code></pre>
  
  <p>All my questions belong to the "header" option and it's values, and how to correctly format it and write it. The PHP documentation, discussion below it and even stackoverflow research yield very inconsistent results.</p>
  
  <p>1) do I have to include the Content-Length header, and if not, will PHP calculate it correctly? <a href="http://php.net/stream_context_create" rel="noreferrer" title="stream_context_create documentation">The documentation</a> does not include it, but I've seen many people include it manually, is it then respected or overwritten by PHP?</p>
  
  <p>2) do I have to pass the header option as a string, or an associative array? Manual says string, majority pass it as a string, but <a href="http://www.php.net/manual/en/function.stream-context-create.php#99353" rel="noreferrer" title="php manual comment">this comment</a> says that if PHP was compiled with --with-curlwrappers option, you have to pass it as an array. This is very inconsistent behavior.</p>
  
  <p>3) when passing as a string, do I have to include terminating <code>\r\n</code> characters? Especially when specifying just one header. Manual does not provide such an example, first comment on manual page does include it, second one does not, again, no clear rule on how to specify this. Does PHP automatically handle both cases?</p>
  
  <p>The server is using PHP 5.3.</p>