title: Can’t read JSON data from fopen in PHP
body: <p>I’m trying to send some JSON data over POST with fopen, using the function below.</p>
  
  <pre><code>function doPostRequest($url, $data, $optional_headers = null)
  {
      $params = array(
          'http' =&gt; array(
              'method'  =&gt; 'POST',
              'content' =&gt; $data,
              'header'  =&gt; 'Content-type: application/json' . "\r\n"
                         . 'Content-Length: ' . strlen($data) . "\r\n"
          )
      );
  
      if ($optional_headers !== null) {
          $params['http']['header'] = $optional_headers;
      }
  
      $ctx = stream_context_create($params);
  
      try {
          $fp = fopen($url, 'rb', false, $ctx);
  
          $response = stream_get_contents($fp);
      } catch (Exception $e) {
          echo 'Exception: ' . $e-&gt;getMessage ();
      }
  
      return $response;
  }
  
  $json_data = array(
      'first_name' =&gt; 'John',
      'last_name'  =&gt; 'Doe'
  );
  
  $content = urlencode(json_encode($json_data));
  
  doPostRequest($url, $content);
  </code></pre>
  
  <p>I don’t get any error, but when I’m trying to decode the data, <code>$input_stream</code> is empty. Why? What am I missing?</p>
  
  <pre><code>$input_stream = file_get_contents('php://input');
  $json = urldecode($input_stream);
  var_dump($input_stream);
  </code></pre>
  
  <p>Edit: I should mention that the code works on a machine with XAMPP installed, but it doesn’t on another one. Could the server configuration have anything to do with it?</p>