tags:

views:

79

answers:

4

I have this question after reading the answer here, what's the difference at all?

Is it possible to submit raw POST with html ?

A: 

_POST assumes it's application/x-www-form-urlencoded or multipart/form-data form values. HTTP_RAW_POST_DATA, if populated, is the raw string. You can also access this with the psuedo-url php://input. You can submit arbitrary POST data (e.g. XML, JSON, HTML) using AJAX.

Matthew Flaschen
$_POST also contains the parsed data when the content type is multipart/form-data, in which case `php://input` cannot be used (same for HTTP_RAW_POST_DATA)
Artefacto
Thanks, @Artefacto.
Matthew Flaschen
A: 

$_POST is an associative array of the incoming POST parameters. PHP creates this for you from the raw HTTP post. If you want to deal with the raw POST data yourself (you might have some binary data sent in the POST), use $HTTP_RAW_POST_DATA.

labratmatt
+1  A: 

$HTTP_RAW_POST_DATA will contain something like:

beans=cheese&spam=eggs&one=two

PHP splits this up for you, and shoves it in the $_POST array. Naively, it does something like this:

$parts = explode('&', $HTTP_RAW_POST_DATA);
foreach ( $parts as $part ) {
    list($key, $value) = explode('=', $part, 2);
    $_POST[$key] = $value;
}

Using JavaScript, which can be embedded into HTML, you can POST anything you like with AJAX. Something like this:

var req = new XMLHttpRequest();
req.open('POST', 'http://www.example.com/my_url' true);
req.send('any data you want');

will allow you to POST arbitrary things to the web server.

tomit
Your example code should do `urldecode()` on `$key` and `$value`.This assumes `<form enctype>` is `application/x-www-form-urlencoded`, which is the default for most HTML forms. There is also the `multipart/form-data` which is used for file uploads.
Zash
I left out some details for clarity, hence me saying "naively".
tomit
+2  A: 

We can divide form submissions in three cases:

  1. Submissions with content type application/x-www-form-urlencoded
  2. Submissions with content type multipart/form-data
  3. Other submissions.

In cases 1 and 3, $HTTP_RAW_POST_DATA contains the raw post data (except if the option is always_populate_raw_post_data is set to false, in which case $HTTP_RAW_POST_DATA is empty in case 1), i.e., the data exactly as the client (usually the browser) has sent it. In case, 1, the data has a form such as

key1=value1&key2=value2&key3[]=value3.1&key3[]=value3.2

PHP automatically parses this, so that $_POST becomes:

$_POST = array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => array("value3.1", "value3.2");
)

The contents of the raw data can also be access through php://input, even in case 1 when always_populate_raw_post_data is set to false. In particular, file_get_contents("php://input") gives the same data $HTTP_RAW_POST_DATA has or would have.

In case 3, in which the POST data is arbitrary, $_POST will be an empty array and $HTTP_RAW_POST_DATA will always be populated.

Case 2 is a special one. In that case, PHP will parse the data and $_POST will get the content of the fields which are not uploaded files, but php://input and $HTTP_RAW_POST_DATA will be unavailable.

Artefacto