10

I'm trying to understand the meaning of raw POST data. The PHP manual page for $HTTP_RAW_POST_DATA just state that this variable contains Raw POST data.

When will this variable be set and what's the meaning of raw POST data?

I understand the $_POST, but I am totally confused with $HTTP_RAW_POST_DATA.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daric
  • 16,229
  • 11
  • 41
  • 58
  • 2
    Have you tried to see what's there? `var_dump($HTTP_RAW_POST_DATA);` or even better `var_dump(file_get_contents("php://input"));` – zerkms Aug 30 '12 at 10:25
  • @zerkms: Yes, It is `NULL`. I tried both and output is `NULL`, `string(0) "" ` respectively – Daric Aug 30 '12 at 10:26
  • 1
    http://stackoverflow.com/questions/3173547/whats-the-difference-between-post-and-raw-post-in-php-at-all – Stefan Neubert Aug 30 '12 at 10:29

5 Answers5

15

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

$_POST can be said to be an outcome after splitting the $HTTP_RAW_POST_DATA variable. PHP splits the raw post data and formats it in the way we see it in the $_POST array. For example:

$HTTP_RAW_POST_DATA looks something like this

key1=value1&key2=value2

then $_POST would look like this:

$_POST = array(
    "key1" => "value1",
    "key2" => "value2",);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ro ko
  • 2,906
  • 3
  • 37
  • 58
2

HTTP is a text-based protocol, so all the data is passed as a strings. When you work with $_POST - you already have the passed data processed for you to be in an array form. This is done by PHP automatically right before the control is passed to your script.

So in the raw POST data there is data as it was passed through the network.

Likely you see a=1&b=2 data, as you see it in URLs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zerkms
  • 249,484
  • 69
  • 436
  • 539
1

One of things you can do with the HTTP protocol is do a POST request which sends some text back to the server.

$HTTP_RAW_POST_DATA will contain that text, no matter what it says.

Most of the time when we do a POST request, we will be adding a "content-type" to the text that is sent. This tells the server what sort of content it is. Most of the time on the web we are sending content type as 'application/x-www-form-urlencoded'.

When a server receives a POST request with this content type marker, the server will know to try to turn the data into a $_POST array so that "test=hello" becomes:

$_POST['test']='hello'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DaveR
  • 2,355
  • 1
  • 19
  • 36
1

$HTTP_RAW_POST_DATA contains the raw POST data like in the following formats:

  • text
  • JSON
  • XML
  • HTML

In general, php://input should be used instead of $HTTP_RAW_POST_DATA. because this feature has been DEPRECATED as of PHP 5.6.0. Relying on this feature is highly discouraged.

Source: php.net - $HTTP_RAW_POST_DATA

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adrian Enriquez
  • 8,175
  • 7
  • 45
  • 64