PHP can receive json data through the file_get_contents method. When receiving such a request, the bottom layer of php will parse the string into the $_POST variable, and then obtain the json data through this variable
file_get_contents("php://input");
Analysis:
php does web page form submission
In the early years of web form submission, $_POST was used to obtain request parameters. In fact, the http request header was in the form of kv value, such as:
#When php receives this kind of request, the php underlying system will parse this string and store it in the $_POST variable, so you can pass $_POST in php Get these parameters.
Upload file http request header
Content-Type has changed to multipart/form-data format. Data acquisition in this format is also processed at the bottom of PHP. Ordinary non-file parameters can also be obtained through $_POST, and file parameters can be obtained through $_FILES.
php gets json format data
The json format data refers to the http header body string which is a json format string. This cannot be obtained through $_POST in PHP, and the bottom layer of PHP does not handle this method. After obtaining the data in the body, you can directly json_decode to get the data in object format.
The above is the detailed content of How to receive json data in php. For more information, please follow other related articles on the PHP Chinese website!