http_get_request_body
(PECL pecl_http >= 0.10.0)
http_get_request_body — Get request body as string
Description
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.
Parameters
Return Values
Returns the raw request body as string on success or NULL
on failure.
See Also
- http_get_request_body_stream() - Get request body as stream
- http_get_request_headers() - Get request headers as array
- the HttpResponse class if you are using PHP 5.1.0 and above
[an error occurred while processing the directive]
- http_cache_etag
- http_cache_last_modified
- http_chunked_decode
- http_deflate
- http_inflate
- http_build_cookie
- http_date
- http_get_request_body_stream
- http_get_request_body
- http_get_request_headers
- http_match_etag
- http_match_modified
- http_match_request_header
- http_support
- http_negotiate_charset
- http_negotiate_content_type
- http_negotiate_language
- ob_deflatehandler
- ob_etaghandler
- ob_inflatehandler
- http_parse_cookie
- http_parse_headers
- http_parse_message
- http_parse_params
- http_persistent_handles_clean
- http_persistent_handles_count
- http_persistent_handles_ident
- http_get
- http_head
- http_post_data
- http_post_fields
- http_put_data
- http_put_file
- http_put_stream
- http_request_body_encode
- http_request_method_exists
- http_request_method_name
- http_request_method_register
- http_request_method_unregister
- http_request
- http_redirect
- http_send_content_disposition
- http_send_content_type
- http_send_data
- http_send_file
- http_send_last_modified
- http_send_status
- http_send_stream
- http_throttle
- http_build_str
- http_build_url
Коментарии
For those of you without the HTTP extension, try:
<?php
$body = @file_get_contents('php://input');
?>
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).
In case this saves anyone else some frustration, the "Content-Length" header decides what will be returned for "php://input". If you leave it out while testing, nothing will be returned for "php://input" or $HTTP_RAW_POST_DATA.
For example, if you are trying to test out a custom SOAP server app and you send a request like this without a Content-Length set:
-----
POST /soap_service.php HTTP/1.1
Authorization: Basic abcdefgh
User-Agent: SOAPy McSOAPclient
Host: example.com
Accept: */*
MIME-Version: 1.0
Content-type: text/xml; charset=utf-8
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cat="http://com.soapy/foobar"><soapenv:Header/><soapenv:Body><foo:ManageBar><foo:task id="1"><foo:getBar type="helloworld"/></foo:task></foo:ManageBar></soapenv:Body></soapenv:Envelope>
----
"php://input" will always return and empty string.
@slave at codegrunt dot com
If you leave out Content-Length and have no Transfer-Encoding, your request is no longer valid.
RFC261 says at chapter 4.3:
"The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's message-headers."
Without those headers, the server has no way of figuring out what the length of the body is. For a response, you could indicate it with a connection close, but obviously if you do that on a request you will never get a response!
So, I assume the PhP behaviour you describe is OK as you cannot expect it to auto-magically repair all sorts of broken requests!
@Tim Trinidad
Reading php://input or using http_get_request_body_stream() must be very similar.
The documentation says:
"This function can not be used after http_get_request_body_stream() if the request method was another than POST."
So it looks very similar to what you describe. The documentation should then read:
"This function can not be used after http_get_request_body_stream() or reading php://input, if the request method was another than POST."