note 98142 added to function.http-get-request-body
| From: | TimTrinidad at osu1 dot php dot net | Date: | Fri, 28 May 2010 00:03:05 +0000 |
| Subject: | note 98142 added to function.http-get-request-body | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169676@lists.php.net to get a copy of this message | ||
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