One article to understand the difference between $ _POST and file_get_contents ("php: // input")

Today, let's talk about the difference between $ _POST, file_get_contents ("php: // input") and $ GLOBALS ['HTTP_RAW_POST_DATA']. These three methods are used to receive post requests, but few people say their What is the difference? Let ’s talk about it

1. $ _POST ['paramName']

Only data submitted by Content-Type: application / x-www-form-urlencoded can be received. PHP will fill the corresponding data of the http request body into the array $ _POST. The result of parsing. (Actually, in addition to the Content-Type, there is also multipart / form-data indicating that the data is form data)

二、file_get_contents(“php://input”)

Suitable for most types of Content-type, php: // input allows to read the raw data of POST. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on memory and does not require any special php.ini settings. php: // input cannot be used for enctype = ”multipart / form-data”.

三、$GLOBALS[‘HTTP_RAW_POST_DATA’];

The $ HTTP_RAW_POST_DATA variable always contains the raw POST data. This variable is only generated when encountering data that does not recognize the MIME type. $ HTTP_RAW_POST_DATA is not available for enctype = ”multipart / form-data” form data.

Pay special attention to this method when the php version is low, and the php.ini configuration is enabled, always_populate_raw_post_data value is On can be used, php7 was abandoned after.

To summarize,
1. Coentent-Type only fills the global data $ _POST with the corresponding data in the http request packet when the values ​​are application / x-www-data-urlencoded and multipart / form-data

2. When the Content-Type type is not recognized by PHP, the corresponding data in the HTTP request package will be filled into the variable $ HTTP_RAW_POST_DATA

3. Only when Coentent-Type is not multipart / form-data, PHP will not fill the corresponding data in the http request packet into php: // input, otherwise other situations will. The length of the filling 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. php: // input data is always the same as $ HTTP_RAW_POST_DATA, but php: // input is more effective than $ HTTP_RAW_POST_DATA, and no special settings are required for php.ini

6. PHP will fill the query_path part of the PATH field into the global variable $ _GET. Usually, the body of the HTTP request submitted by the GET method is empty.

In short:

1. If it is application / x-www-form-urlencoded and multipart / form-data format, use $ _POST;

2. If text / xml, application / json, soap are not available, use file_get_contents ('php: // input');

After reading this, you should understand why we use file_get_contents ("php: // input") more when we interface with third-party platforms.

Guess you like

Origin www.cnblogs.com/qqshuazuan/p/12739290.html