note 98142 added to function.http-get-request-body

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
It seems that there is some weird behavior when using http_get_request_body() with fopen('php://input'). Specifically, reading the input with the fopen('php://input') routine before calling http_get_request_body() on a PUT HTTP request.

Here are some examples:

A POST request:
*********************************************************
PUT http://example.com/requestbodytest.php HTTP/1.1
Host: example.com
Keep-Alive: 300
Connection: keep-alive
Content-Type: text/xml
Content-Length: 58

<?xml version="1.0" encoding="utf-8" ?>
<body>test</body>
*********************************************************

with the following script:
*********************************************************
<?php
$body = '';
$fh   = @fopen('php://input', 'r');
if ($fh)
{
  while (!feof($fh))
  {
    $s = fread($fh, 1024);
    if (is_string($s))
    {
      $body .= $s;
    }
  }
  fclose($fh);
}
print("-------------- PHP Input Stream ----------------\n$body\n\n");

$body2 = http_get_request_body();
print("---------- http_get_request_body() -------------\n$body2\n\n");

?>
*********************************************************

outputs this:
*********************************************************
-------------- PHP Input Stream ----------------
<?xml version="1.0" encoding="utf-8" ?>
<body>test</body>

---------- http_get_request_body() -------------
<?xml version="1.0" encoding="utf-8" ?>
<body>test</body>
*********************************************************

The same request to the same script using an HTTP PUT request, however, outputs this:
*********************************************************
-------------- PHP Input Stream ----------------
<?xml version="1.0" encoding="utf-8" ?>
<body>test</body>

---------- http_get_request_body() -------------
*********************************************************

It seems a valid workaround is to put a call to http_get_request_body() to cache the body content before an expected read to php://input (i.e. simply calling the function without manually storing the result caches the content for subsequent calls).
----
Server IP: 208.69.120.55
Probable Submitter: 75.103.2.138
----
Manual Page -- http://www.php.net/manual/en/function.http-get-request-body.php
Edit        -- https://master.php.net/note/edit/98142
Del: integrated  -- https://master.php.net/note/delete/98142/integrated
Del: useless     -- https://master.php.net/note/delete/98142/useless
Del: bad code    -- https://master.php.net/note/delete/98142/bad+code
Del: spam        -- https://master.php.net/note/delete/98142/spam
Del: non-english -- https://master.php.net/note/delete/98142/non-english
Del: in docs     -- https://master.php.net/note/delete/98142/in+docs
Del: other reasons-- https://master.php.net/note/delete/98142
Reject      -- https://master.php.net/note/reject/98142
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.