The purpose of this article:
Why do I usually use file_get_contents("php://input") instead of $_POST when I interface with third-party platforms when receiving http request packets?
file_get_contents : read the entire file into a string
When writing an interface with php, the requested data is usually sent to the specified request address in the form of json. At this time, file_get_contents('php://input') is mainly used to obtain the original data of the request. Compared with $HTTP_RAW_POST_DATA, php://input puts less pressure on memory and does not require any special php.ini settings.
Pay attention to two points:
- The data submission method should be POST
- php://input cannot be used to receive data submitted by enctype="multipart/form-data"
1. Give a simple example:
- submit Form
<form action="" method="POST">
姓名: <input type="text" name="name" value="tom" /><br />
年龄:<input type="text" name="age" value="22" /><br />
<input type="submit" value="Submit" />
</form>
- The back-end controller obtains the original data stream of the request through file_get_contents('php://input')
<?php
$content = file_get_contents('php://input', 'r');
echo $content;
//输出name=tom&age=22
?>
2. In project applications, php://input can be used, such as taking photos with a camera, uploading and saving. After the client takes a picture, the image stream is sent to the server, and the server can get the image stream by using file_get_getcontents('php://input').
$_POST
$_POST can only receive data submitted by Content-Type: application/x-www-form-urlencoded, PHP will fill the corresponding data of the http request body into the array _POST, and the data in the _POST array is parsed by urldecode() result. In addition to the Content-Type, form data of multipart/form-data type can also be received with $_POST.
The four common ways to submit data are as follows:
Definition and usage
The enctype attribute specifies how the form data should be encoded before being sent to the server . By default, the form data will be encoded as "application/x-www-form-urlencoded". That is, before sending to the server, all characters will be encoded (spaces are converted to "+" plus signs, and special symbols are converted to ASCII HEX values).
enctype attribute value
Content-Type value | description |
---|---|
application/x-www-form-urlencoded | Encode all characters before sending (default) |
multipart/form-data | The characters are not encoded. This value must be used when using a form that contains a file upload control. |
text/plain | Spaces are converted to "+" plus signs, but special characters are not encoded. |
text/xml |
A remote call specification that uses HTTP as the transport protocol and XML as the encoding method. |
to sum up:
1. Only when the value of Coentent-Type is application/x-www-data-urlencoded and multipart/form-data, PHP will fill the corresponding data in the http request packet into the global variable $_POST.
2. When the Content-Type type is not recognized by PHP, the corresponding data in the http request packet will be filled into the variable $HTTP_RAW_POST_DATA.
3. Only when the Coentent-Type is multipart/form-data, PHP will not fill the corresponding data in the http request packet into php: //input, otherwise it will be in other situations. The length to be filled in is specified by Coentent-Length.
4. Only when the Content-Type is application/x-www-data-urlencoded, the php://input data is consistent with the $_POST data.
5. If it cannot be obtained, for example, Coentent-Type is text/xml, application/json, soap, please use file_get_contents('php://input');