Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To access the entity body of a POST or PUT request (or any other HTTP method):</p> <pre><code>$entityBody = file_get_contents('php://input'); </code></pre> <p>Also, the <code>STDIN</code> constant is an already-open stream to <code>php://input</code>, so you can alternatively do:</p> <pre><code>$entityBody = stream_get_contents(STDIN); </code></pre> <p>From the <a href="http://php.net/manual/en/wrappers.php.php" rel="noreferrer">PHP manual entry on I/O streams<sup><em>docs</em></sup></a>:</p> <blockquote> <p><em>php://input</em> is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use <em>php://input</em> instead of <code>$HTTP_RAW_POST_DATA</code> as it does not depend on special php.ini directives. Moreover, for those cases where <code>$HTTP_RAW_POST_DATA</code> is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. <em>php://input</em> is not available with enctype="multipart/form-data".</p> </blockquote> <p>Specifically you'll want to note that the <code>php://input</code> stream, regardless of how you access it in a web SAPI, <em>is not seekable</em>. This means that it can only be read once. If you're working in an environment where large HTTP entity bodies are routinely uploaded you may wish to maintain the input in its stream form (rather than buffering it like the first example above).</p> <p>To maintain the stream resource something like this can be helpful:</p> <pre><code>&lt;?php function detectRequestBody() { $rawInput = fopen('php://input', 'r'); $tempStream = fopen('php://temp', 'r+'); stream_copy_to_stream($rawInput, $tempStream); rewind($tempStream); return $tempStream; } </code></pre> <p><code>php://temp</code> allows you to manage memory consumption because it will transparently switch to filesystem storage after a certain amount of data is stored (2M by default). This size can be manipulated in the php.ini file or by appending <code>/maxmemory:NN</code>, where <code>NN</code> is the maximum amount of data to keep in memory before using a temporary file, in bytes.</p> <p>Of course, unless you have a really good reason for seeking on the input stream, you shouldn't need this functionality in a web application. Reading the HTTP request entity body once is usually enough -- don't keep clients waiting all day while your app figures out what to do.</p> <p>Note that php://input is not available for requests specifying a <code>Content-Type: multipart/form-data</code> header (<code>enctype="multipart/form-data"</code> in HTML forms). This results from PHP already having parsed the form data into the <code>$_POST</code> superglobal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload