Home > Article > Backend Development > php gets POST JSON data
When passing data to the php program through ajax, you will find that you cannot obtain the data using $_POST. However, you can see in firebug that there is actually json data in the post.
When using print_r(file_get_contents("php:/ /input")), you can get json data
So what is php://input?
Introduction to php://input, the PHP official manual document has a paragraph that makes it very clear Overview.
php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart /form-data".
" Translated, this is:
"php://input can read unprocessed POST data. Compared to $HTTP_RAW_POST_DATA, it puts less pressure on memory and does not require special php.ini settings. php://input cannot be used for enctype=multipart/form-data".
1. When the Content-Type value is application/x-www-form-urlencoded, php will fill in the corresponding data of the http request body into Array $_POST, the data filled in the $_POST array is the result of urldecode() analysis (in fact, in addition to the Content-Type, there is also multipart/form-data indicating that the data is form data, which we will introduce later)
2. php://input data, as long as Content-Type is not multipart/form-data (this condition will be introduced later). Then php: //input data is consistent with the http entity body part. The length of the consistent data is specified by Content-Length 3. Only when the Content-Type is application/x-www-form-urlencoded and the submission method is the POST method, the $_POST data and php://input data are the same. They are "consistent" (with quotation marks, indicating that their formats are inconsistent and their contents are consistent). In other cases, they are inconsistent. 4. php://input cannot read the $_GET data because the $_GET data is written as query_path. The PATH field in the http request header is not written in the body part of the http request. I believe you already have a certain depth of understanding of php://input. So what is $http_raw_post_data? http_raw_post_data is a global variable built into PHP. It is used by PHP to fill the POST data into the variable $http_raw_post_data as it is when the Content-Type is not recognized. It also cannot read the Content-Type of multipart/form. -data POST data. You need to set the always_populate_raw_post_data value in php.ini to On, so that PHP will always fill in the POST data into the variable $http_raw_post_data. Study Note 1, Coentent-Type only takes the value application/x-www. In the two cases of -data-urlencoded and multipart/form-data, PHP will fill in the corresponding data in the http request packet into the global variable $_POST
2. When the Content-Type type cannot be recognized by PHP, it will The corresponding data in the http request packet is filled in the variable $HTTP_RAW_POST_DATA
3. Only when the Coentent-Type is not multipart/form-data, PHP will not fill in the corresponding data in the http request packet into php://input. Otherwise everything else will be. The length of the padding, specified by Coentent-Length.
4. Only when the Content-Type is application/x-www-data-urlencoded, the php://input data is consistent with the $_POST data.
5. php://input data is always the same as $HTTP_RAW_POST_DATA, but php://input is more efficient than $HTTP_RAW_POST_DATA and does not require special settings for php.ini
6. PHP will replace the query_path part of the PATH field , fill in the global variable $_GET. Normally, the body of an http request submitted by the GET method is empty.