User Contributed Notes 4 notes

@slave at codegrunt dot comIf 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 TrinidadReading 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."

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:

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.

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>

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).