Home > Article > Backend Development > A brief discussion on how PHP receives POST data_PHP tutorial
This article briefly introduces 3 ways for PHP to receive post data, and attaches a simple example, if necessary Friends can refer to it
Normally, users use browser web forms to post data to the server. We use PHP to receive the data that users POST to the server and process it appropriately. But in some cases, if the user uses client software to send post data to the server PHP program and cannot use $_POST to identify it, how should it be handled?
$_POST method to receive data
The $_POST method is an array of variables passed through the HTTP POST method and is an automatic global variable. For example, if you use $_POST['name'], you can receive data posted from web forms and web pages asynchronously, that is, $_POST can only receive data submitted with the document type Content-Type: application/x-www-form-urlencoded.
Receive data via $GLOBALS['HTTP_RAW_POST_DATA']
If the data posted is not a document type that PHP can recognize, such as text/xml or soap, etc., we can use $GLOBALS['HTTP_RAW_POST_DATA'] to receive it. The $HTTP_RAW_POST_DATA variable contains the raw POST data. This variable is generated only when data of unrecognized MIME types is encountered. $HTTP_RAW_POST_DATA is not available for enctype="multipart/form-data" form data. In other words, using $HTTP_RAW_POST_DATA cannot receive data posted from web forms.
php://input method to receive data
If a better way to access the raw POST data is php://input. php://input allows reading the raw data of a POST. It puts less pressure on memory than $HTTP_RAW_POST_DATA and does not require any special php.ini settings, while php://input cannot be used with enctype="multipart/form-data".
For example, the user uses a client application to post a file to the server. We don’t care about the content of the file, but we want to save the file completely on the server. We can use the following code:
?
2 3
|
|
1 2 3 4 5 6 |
post.php
?
2 3 11 12
13
14
|
header("Content-type:text/html;charset=utf-8");
echo '$_POST接收: '; print_r($_POST); echo ' '; echo '$GLOBALS['HTTP_RAW_POST_DATA']接收: '; print_r($GLOBALS['HTTP_RAW_POST_DATA']); echo ' '; echo 'php://input接收: '; $data = file_get_contents('php://input'); print_r(urldecode($data)); |