Examples of three methods for php to obtain POST data: three methods for post
Three methods for php to obtain POST data
Method 1, $ _ POST
$ _ POST or $ _ REQUEST stores PHP formatted data in the form of key => value.
Method 2: Use file_get_contents ("php: // input ")
For POST data without Content-Type specified, you can use file_get_contents ("php: // input"); to obtain the original data. In fact, this method is used to receive any POST data using PHP. Content-Type, including binary file streams, is also feasible.
Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on memory and does not require any special php. ini settings.
Php: // input cannot read POST data whose Content-Type is multipart/form-data. You must set always_populate_raw_post_data in php. ini to On.
Php: // input cannot read $ _ GET data. The reason is that the $ _ GET data is written in the PATH field of the http Request header as query_path, rather than in the http Request body.
Method 3: Use the global variable $ GLOBALS ['HTTP _ RAW_POST_DATA ']
In $ GLOBALS ['HTTP _ RAW_POST_DATA '], raw POST data is stored.
However, whether the POST data is saved in $ GLOBALS ['HTTP _ RAW_POST_DATA '] depends on the centent-Type setting. Only when PHP cannot recognize the Content-Type, the POST data is filled in the variable $ GLOBALS ['HTTP _ RAW_POST_DATA '] as is, such as Content-Type = application/x-www-form-urlencoded, the variable is empty.
In addition, it cannot read POST data whose Content-Type is multipart/form-data. You also need to set php. in ini, if the always_populate_raw_post_data value is On, PHP will always fill in the POST data with the variable $ http_raw_post_data.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!