Home > Article > Backend Development > Why can PHP only accept get but not POST value?
php generally uses get or post values. This article mainly shares with you why php can only accept get values but cannot accept POST values. I hope it can help everyone.
1. Several methods for PHP to obtain POST data
Method 1. The most common method is: $_POST['fieldname'];
Explanation: Can only receive data submitted by Content-Type: application/x-www-form-urlencoded
Explanation: That is the data from the form POST
Method 2, file_get_contents("php://input");
Description:
Allows reading of the original data of POST.
(When using the interface testing tool, you can only use this method if the header is set to content-type: application/json.)
Compared with $HTTP_RAW_POST_DATA, it gives It puts less pressure on memory and does not require any special php.ini settings.
php://input cannot be used with enctype="multipart/form-data".
Explanation:
For POST data without specified Content-Type, you can use file_get_contents("php://input"); to obtain the original data.
In fact, this method can be used to receive any POST data using PHP. Regardless of Content-Type, including binary file streams is also acceptable.
So using method 2 is the safest method
Method 3, $GLOBALS['HTTP_RAW_POST_DATA'];
Instructions:
Total The $HTTP_RAW_POST_DATA variable is generated to contain the raw POST data.
This variable is only generated when data of unrecognized MIME type is encountered.
$HTTP_RAW_POST_DATA is not available for enctype="multipart/form-data" form data
If the posted data is not recognized by PHP, you can use $GLOBALS['HTTP_RAW_POST_DATA'] to receive it,
such as text /xml or soap, etc.
Explanation:
$GLOBALS['HTTP_RAW_POST_DATA'] stores the original data from POST.
$_POST or $_REQUEST stores data formatted by PHP in the form of key=>value.
But whether the POST data is saved in $GLOBALS['HTTP_RAW_POST_DATA'] depends on the centent-Type setting, that is, when POSTing data, the Content-Type must be explicitly specified: application/x-www-form-urlencoded, POST The data will be stored in $GLOBALS['HTTP_RAW_POST_DATA'].
Related recommendations:
Elaborate on the difference between get and post
The above is the detailed content of Why can PHP only accept get but not POST value?. For more information, please follow other related articles on the PHP Chinese website!