Examples of three methods for php to obtain POST data: three methods for post

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.