PHP "PHP://Input" VS $_Post

PHP php://input vs $_POST

The reason is that php://input returns all the raw data after the HTTP-headers of the request, regardless of the content type.

The PHP superglobal $_POST, only is supposed to wrap data that is either

  • application/x-www-form-urlencoded (standard content type for simple form-posts) or
  • multipart/form-data (mostly used for file uploads)

This is because these are the only content types that must be supported by user agents. So the server and PHP traditionally don't expect to receive any other content type (which doesn't mean they couldn't).

So, if you simply POST a good old HTML form, the request looks something like this:

POST /page.php HTTP/1.1

key1=value1&key2=value2&key3=value3

But if you are working with Ajax a lot, this probaby also includes exchanging more complex data with types (string, int, bool) and structures (arrays, objects), so in most cases JSON is the best choice. But a request with a JSON-payload would look something like this:

POST /page.php HTTP/1.1

{"key1":"value1","key2":"value2","key3":"value3"}

The content would now be application/json (or at least none of the above mentioned), so PHP's $_POST-wrapper doesn't know how to handle that (yet).

The data is still there, you just can't access it through the wrapper. So you need to fetch it yourself in raw format with file_get_contents('php://input') (as long as it's not multipart/form-data-encoded).

This is also how you would access XML-data or any other non-standard content type.

$_POST vs. $HTTP_RAW_POST_DATA vs file_get_contents(php://input)?

  1. $_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.
  2. 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.
  3. $HTTP_RAW_POST_DATA - in theory it is the same as the above but depends on php.ini. (DEPRECATED, SEE COMMENT)

I always use method #2 instead of #3 when I need non application/www-url-encoded input.

file_get_contents(php://input) or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?

Actually php://input allows you to read raw request body.

It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.
From Reference

php://input is not available with enctype="multipart/form-data".

$_POST empty however php://input shows values present

Wow so I managed to figure it out.

I took the POST data that i got via php://input and created a JSON variable with the data. Then after some testing I tried to see if treating it like a nested array after performing the $posted_data = json_decode(file_get_contents("php://input"));.

So i called the data using the following:

$posted_data[0]->emailAddress

And that worked... hope this can help anyone in the future and save the amount of hours i've just wasted :)

HTML/PHP input field - If $_POST is set, value = something, otherwise use placeholder?

<input type="text" name="nickname" placeholder="nickname" value="<?php echo isset($_POST["nickname"]) ? $_POST["nickname"] : ""; ?>" maxlength="30" required="required">

PHP php://input vs $_POST

The reason is that php://input returns all the raw data after the HTTP-headers of the request, regardless of the content type.

The PHP superglobal $_POST, only is supposed to wrap data that is either

  • application/x-www-form-urlencoded (standard content type for simple form-posts) or
  • multipart/form-data (mostly used for file uploads)

This is because these are the only content types that must be supported by user agents. So the server and PHP traditionally don't expect to receive any other content type (which doesn't mean they couldn't).

So, if you simply POST a good old HTML form, the request looks something like this:

POST /page.php HTTP/1.1

key1=value1&key2=value2&key3=value3

But if you are working with Ajax a lot, this probaby also includes exchanging more complex data with types (string, int, bool) and structures (arrays, objects), so in most cases JSON is the best choice. But a request with a JSON-payload would look something like this:

POST /page.php HTTP/1.1

{"key1":"value1","key2":"value2","key3":"value3"}

The content would now be application/json (or at least none of the above mentioned), so PHP's $_POST-wrapper doesn't know how to handle that (yet).

The data is still there, you just can't access it through the wrapper. So you need to fetch it yourself in raw format with file_get_contents('php://input') (as long as it's not multipart/form-data-encoded).

This is also how you would access XML-data or any other non-standard content type.



Related Topics



Leave a reply



Submit