Home>Article>Backend Development> PHP gets data in different formats in HTTP POST
The POST method in the HTTP protocol has multiple formats of data protocols, which are identified by different Content-types in the HTTP header. Commonly used ones are
application/x-www-form- urlencoded
, which is the most common, is the format of the from form. In the HTTP head, it isContent-Type: application/x-www-form-urlencoded.
multipart/form-data
, this is used to upload files, in the HTTP head isContent-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Raw This is not particularly commonly used. The transmitted data has only one segment in the HTTP body and is not stored in the form of key-value pairs. In the HTTP head, it isContent-Type: application/json
,Content-Type: text
,Content-Type: application/xml
,Content-Type: text/xml
, etc. For
The data of this form can be obtained directly in php using$_POST['name']
. There is nothing Special
Data in this format can be obtained by using$_POST['name']
in php Data can be obtained using$_FILES['file']
.For data in Raw format, it cannot be obtained using the above two methods, and other methods need to be used.
1. Use
file_get_contents("php://input")Get; write a simple php file to test it
Test it with postman<?php $test=file_get_contents("php://input"); echo $test;
No problem, you can receive it
2. Use
$GLOBALS['HTTP_RAW_POST_DATA']Receive
Test it with postman<?php $test=$GLOBALS['HTTP_RAW_POST_DATA']; echo $test;
Damn it, something went wrong, the prompt was not found
HTTP_RAW_POST_DATAWhat the hell is this array index? After some Google, I saw this paragraph on the official website of PHP Words
It turns out that
HTTP_RAW_POST_DATAhas been abandoned in php5.6 and has been deleted in php7.0 and later versions. I use The php version is 7.2, something must be wrongOkay, then just use
Get itIn actual development, frameworks are generally used. I use thinkphp more. In tp5.0, you can use the getInput() function of Request to obtain the data in Raw
getInput(); } }
Test it
No problem, you can obtain it normally
The method of obtaining HTTP POST data in PHP will be introduced here. I hope it will be helpful to friends in need!
The above is the detailed content of PHP gets data in different formats in HTTP POST. For more information, please follow other related articles on the PHP Chinese website!