Home >Backend Development >PHP Tutorial >Detailed explanation of the example of receiving raw data in PHP post
This article mainly shares with you the detailed examples of receiving raw data in post in PHP. I hope it can help everyone.
Usually, 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
$_POST method is an array of variables passed through the HTTP POST method, which 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.
$GLOBALS['HTTP_RAW_POST_DATA'] method to receive data
If the data sent by post 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. 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. That is to say, using $HTTP_RAW_POST_DATA cannot receive the data posted from the web form.
php://input method to receive data
If the better way to access the original 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 :
$input = file_get_contents('php://input'); file_put_contents($original, $input); //$original为服务器上的文件
The above code uses file_get_contents('php://input') to receive post data, and then writes the data to the $original file. It is actually understandable In order to upload a file from the client to the server, there are many such applications, especially when our PHP development needs to jointly develop products with C, C++ and other application development.
Related recommendations:
php solves the problem of loss when posting large amounts of data
curl get post request parsing in php
How to handle exceptions displayed when post submits data
The above is the detailed content of Detailed explanation of the example of receiving raw data in PHP post. For more information, please follow other related articles on the PHP Chinese website!