Reputation: 662
I am searching the exact definition of this, but I still can't find a satisfactory definition.
I was using the NuSOAP web service, and there is a line,
$server->service($HTTP_RAW_POST_DATA);
What is the purpose of $HTTP_RAW_POST_DATA here?
As per my research, it is for collecting raw POST data, but I am not satisfied with the answer. What is the exact meaning of the raw POST data?
Upvotes: 1
Views: 1749
Reputation: 97908
The majority of the time, when we're working with HTTP POST requests, we're looking at the result of a form submission from the browser. This will format the data in a standard way (either "query-string encoded" or "multipart/form-data") which encodes a set of key-value pairs. PHP automatically parses any requests in this format into the super-global array $_POST
.
However, the actual HTTP request body is just a piece of text, and in some cases we want to treat it as such. For instance, a SOAP request uses an XML document as the body of the POST request. $HTTP_RAW_POST_DATA
gives you access to this text - it is "raw" because it hasn't been processed, it's just given back to you as a string.
It's worth noting that the $HTTP_RAW_POST_DATA
is an old way of accessing this, and the correct way in newer versions of PHP is to use file_get_contents('php://input');
where php://input
is a virtual file which represents that same raw data.
Upvotes: 0
Reputation: 26268
An HTTP request consists of two parts. A set of headers and a body.
The headers include things like the URL being requested and caching control helpers (such as "I have a version of this from yesterday, only give me a new one if there are changes, OK?").
The body may or may not appear depending on the type of request. POST requests have bodies.
The body can be in any format the client likes. One of the headers will tell the server what the format is.
There are a couple of formats used by HTML forms, and PHP knows how to parse these and put the data into $_POST.
If the data is in another format, such as JSON, or if the data doesn't conform to PHP's quirks (such as the rules for having [] on the end of keys with the same name) then you might want to access the data directly so you can parse it yourself.That is the raw POST data.
$_POST
contains URL encoded (application/www-url-encoded) variables that are posted to your script and PHP decodes them for you. You use this one when you deal with HTML FORM data.
file_get_contents("php://input")
- gets the raw POST data and you need to use this when you write APIs and need XML/JSON/... input that cannot be decoded to $_POST by PHP.
$HTTP_RAW_POST_DATA
- in theory it is the same as the above but depends on php.ini. and is no longer available in PHP 7
Upvotes: 2
Reputation: 944084
An HTTP message consists of a series of headers and a body (which can be required/optional/forbidden depending on the headers).
For the URL http://stackoverflow.com/questions/43519007/usage-of-http-raw-post-data
, the body is the HTML used to generate the document you are reading.
If you make a GET request for it, then there is no request body.
If you make a POST request then there will be. If you submit a form, it will be encoded using one of the formats you can specify with the enctype
attribute.
Normally, in PHP, when you want to work with that data, you would use $_POST
which contains the content after the body has been decoded.
The raw body is the body before it has been decoded.
Upvotes: 0