Home  >  Article  >  Backend Development  >  Is there a way to get post parameters in php?

Is there a way to get post parameters in php?

L
LOriginal
2020-06-01 16:52:113356browse

Is there a way to get post parameters in php?

Several ways for PHP to obtain post parameters

The data type recognized by PHP by default is application/x-www .form-urlencoded standard data type.

1. $_POST['paramName'] Only when the Content-Type is application/x-www-form-urlencoded or multipart/form-data, PHP will change the HTTP request packet The corresponding part of the body data is filled in the $_POST global variable, and PHP ignores other situations. The data filled into the $_POST array is the result of urldecode() parsing.

2. file_get_contents("php://input") is suitable for most types of Content-type

php://input allows reading the raw data of POST. It puts less pressure on memory than $HTTP_RAW_POST_DATA and does not require any special php.ini settings. php://input cannot be used with enctype="multipart/form-data".

3. $GLOBALS['HTTP_RAW_POST_DATA']; always generates $HTTP_RAW_POST_DATA variable containing the original POST data. This variable is only generated when data of an unrecognized MIME type is encountered. $HTTP_RAW_POST_DATA is not available for enctype="multipart/form-data" form data.

If the posted data is not recognized by PHP, you can use $GLOBALS['HTTP_RAW_POST_DATA'] to receive it, such as text/xml or soap, etc. You 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.

Look at the official documentation, in higher versions, this variable $HTTP_RAW_POST_DATA is deprecated
This feature wasDEPRECATEDin ​​PHP 5.6.0, andREMOVEDas of PHP 7.0.0. In general,php://inputshould be used instead of $HTTP_RAW_POST_DATA.

Summary:

1, Coentent-Type is only used when the value 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 PHP does not recognize the Content-Type type, it will fill in the corresponding data in the http request packet into the variable $. HTTP_RAW_POST_DATA

3, only when the Coentent-Type is not multipart/form-data, PHP will fill in the corresponding data in the http request packet into php: //input, otherwise it will be in other cases. The length of the padding, 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. They only read data whose Content-Type is not multipart/form-data, but php://input is more efficient than $HTTP_RAW_POST_DATA. , and no special setting is required for php.ini

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

7, 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.

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

9. If it cannot be obtained, such as text/xml, application /json, soap, use file_get_contents('php://input');

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of Is there a way to get post parameters in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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