Home php教程 PHP源码 Three ways to get POST data with PHP

Three ways to get POST data with PHP

Nov 08, 2016 pm 01:59 PM

Method 1, $_POST

$_POST or $_REQUEST stores 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 obtain 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 in the PATH field of the http request header as query_path, rather than in the body part of the http request.

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

The original data from POST is stored in $GLOBALS[‘HTTP_RAW_POST_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, the POST data will be filled in the variable as it is. In $GLOBALS['HTTP_RAW_POST_DATA'], when Content-Type=application/x-www-form-urlencoded, this variable is empty.
In addition, it cannot 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 into the variable $http_raw_post_data.


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)