Home > Backend Development > PHP Tutorial > Detailed explanation of three methods of obtaining POST data in PHP

Detailed explanation of three methods of obtaining POST data in PHP

墨辰丷
Release: 2023-03-28 10:52:02
Original
8145 people have browsed it

This article introduces three methods for POST to obtain POST data. The first method is $_POST, the second method is to use file_get_contents, and the third method is to use the global variable $GLOBALS. Please see the following instructions for details

Three methods for php to obtain POST data

Method 1, $_POST

##$_POST or $_REQUEST What is stored is the data formatted by PHP in the form of key=>value.

Method 2, use file_get_contents("php://input")

For POST data without specified Content-Type, you can Use file_get_contents("php://input"); to get the original data. In fact, any data received by POST using PHP uses this method. Regardless of 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 need to set the always_populate_raw_post_data value in php.ini to On.


php://input cannot read $_GET data. This is because the $_GET data is written as query_path in the PATH field of the http request header (header), rather than in the body part of the http request.

Method three, use the global variable $GLOBALS['HTTP_RAW_POST_DATA']

The POST is stored in $GLOBALS['HTTP_RAW_POST_DATA'] of raw data.


But 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, will the POST be saved? The passed data is filled into the variable $GLOBALS['HTTP_RAW_POST_DATA'] as it is. When Content-Type=application/x-www-form-urlencoded, the variable is empty.


In addition, it is also unable to read POST data whose Content-Type is multipart/form-data. You also need to set the always_populate_raw_post_data value in php.ini to On so that PHP will always fill in the POST data. Variable $http_raw_post_data.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP method for developing WeChat cash red envelope function

PHP uses continue to realize the attention points of skipping the remaining code in this loop

PHP WeChat simulated login and send messages to users

The above is the detailed content of Detailed explanation of three methods of obtaining POST data in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template