PHP Doku:: Get request body as string - function.http-get-request-body.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteHTTPHTTP Funktionenhttp_get_request_body

Ein Service von Reinhard Neidl - Webprogrammierung.

HTTP Funktionen

<<http_get_request_body_stream

http_get_request_headers>>

http_get_request_body

(PECL pecl_http >= 0.10.0)

http_get_request_bodyGet request body as string

Beschreibung

string http_get_request_body ( void )

Get the raw request body (e.g. POST or PUT data).

This function can not be used after http_get_request_body_stream() if the request method was another than POST.

Parameter-Liste

Rückgabewerte

Returns the raw request body as string on success or NULL on failure.

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
Tim Trinidad
28.05.2010 2:03
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).
neil at foo dot co dot za
29.02.2008 10:40
Like php://input and http_get_request_body_stream(), http_get_request_body() doesn't seem to work with enctype="multipart/form-data".
osborn dot steven at gmail dot com
23.08.2007 20:01
For those of you without the HTTP extension, try:
<?php
  $body
= @file_get_contents('php://input');
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)