©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PECL pecl_http >= 0.10.0)
http_get_request_body — Get request body as string
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.
Returns the raw request body as string on success or NULL
on failure.
[#1] jimfr06 at gmail dot com [2013-06-24 22:11:36]
@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."
[#2] slave at codegrunt dot com [2013-05-31 20:31:32]
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: **" and thus receives application/xhtml+xml if you use the current sample from the documentation.
<?php
$bestContentType = http_negotiate_content_type(array('application/xhtml+xml', 'text/html'),
$contentTypes);
$contentType = (empty($contentTypes) ? 'text/html' : $bestContentType);
http_send_content_type($contentType);
?>
Note that at the time of writing, Opera accepts text/html with this approach, because it places application/xhtml+xml with a lower qualifier.
[#3] Pieter van Beek [2007-07-03 09:38:37]
The documentation says:
Returns the negotiated content type or the default content type (i.e. first array entry) if none match.
It would seem that in fact, this method returns the empty string or NULL if none match.