title: HTTP request failed in PHP Bad Request
body: <p>I can request a URL web service 'WS' directly from the browser, but when I use file_get_contents() or fopen methods in my code I get an error message. Does someone have a solution without using curl?</p>
  
  <pre><code>public function sendHttpRequest($data) {
     ini_set('user_agent', 'PHP');
  
      $context_options = array(
          'http' =&gt; array(
              'method' =&gt; 'POST',
              'header' =&gt; "Content-Type: application/x-www-form-urlencoded\r\n",
              'content' =&gt; json_encode($data)
          )
      );
  
  
      $context = stream_context_create($context_options);
  
      //line error "line 196"
      $result = file_get_contents($this-&gt;WS, false, $context);
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
     in /mnt/hgfs/case/src/bat/abc/send-entry.php on line 196</p>
    
    <p>NULL</p>
  </blockquote>
  
  <p>i've changed the context options to this, but still getting the <code>HTTP/1.1 400 Bad Request</code></p>
  
  <pre><code>$context_options = array(
          'http' =&gt; array(
              'method' =&gt; 'POST',
              'header' =&gt; "Date: " . gmdate("D, d M Y H:i:s T", time()) . "\r\n"
              . get_headers($this-&gt;WS)[2] . "\r\n"
              . " x-powered-by: PHP/" . phpversion() . "\r\n"
              . " " . get_headers($this-&gt;WS)[5] . "\r\n"
              . " Pragma: no-cache" . "\r\n"
              . " Content-Length: " . strlen($this-&gt;content) . "\r\n"
              . " Content-Type: text/xml" . "\r\n"
              . " " .get_headers($this-&gt;WS)[9] . "\r\n"
              . " Connection: close",
              'content' =&gt; $this-&gt;content
          )
  
  var_dump(get_headers($this-&gt;WS))
  </code></pre>
  
  <p>the data is sent to a server with adress <a href="http://192.168.xxx.xxx/WS" rel="nofollow">http://192.168.xxx.xxx/WS</a></p>
  
  <p>here is a var_dump of the array to send</p>
  
  <pre><code>array(16) {
    ["method"]=&gt;
    string(24) "WS"
    ["LOGIN"]=&gt;
    string(12) "xxx"
    ["DATE1"]=&gt;
    string(10) "1970-01-01"
    ["DATE2"]=&gt;
    string(0) ""
    ["TRANSMISSION"]=&gt;
    INT(8)
    ["REF"]=&gt;
    int(206)
    ["FORMAT"]=&gt;
    int(7)
    ["DOMAIN"]=&gt;
    int(3)
    ["TYPE1"]=&gt;
    int(15)
    ["TYPE2"]=&gt;
    NULL
    ["NAME"]=&gt;
    string(12) "JDOE"
    ["ADRESSE"]=&gt;
    string(0) ""
    ["ADRESSE2"]=&gt;
    string(0) ""
    ["ADRESSE3"]=&gt;
    string(0) ""
    ["ADRESSE4"]=&gt;
    string(0) ""
    ["REF2"]=&gt;
    string(0) ""
  }
  </code></pre>
  
  <p>here is the full message error</p>
  
  <blockquote>
    <p>Warning: file_get_contents(<a href="http://192.168.xxx.xxx/WS" rel="nofollow">http://192.168.xxx.xxx/WS</a>): failed to open   stream: HTTP request failed! HTTP/1.1 400 Bad Request
     in /mnt/hgfs/case/src/bat/abc/send-entry.php on line 182</p>
  </blockquote>
  
  <p>the working code:</p>
  
  <pre><code>    $context_options1 = array(
          'http' =&gt; array(
              'method' =&gt; 'POST',
              'header' =&gt; "Date: " . gmdate("D, d M Y H:i:s T", time())
              . "Accept: application/xml"
              . "Content-Type: application/xml"
              . "Content-Length: " . strlen($this-&gt;content),
              'content' =&gt; $this-&gt;content
          )
      );
  </code></pre>