Several methods for PHP to obtain POST data aggregation _ PHP Tutorial

Source: Internet
Author: User
Tags form post
PHP summarizes several methods for obtaining POST data. This article provides a summary of several methods for PHP to obtain POST data. it introduces several common methods for PHP to obtain POST data. here, we will share with you some suggestions. I. summary of several methods for PHP to obtain POST data

This article provides a summary of several common methods for PHP to obtain POST data. we will share this article with you. if you need it, please refer to it.

I. several methods for PHP to obtain POST data

Method 1. the most common method is: $ _ POST ['fieldname'];

Note: only data submitted by Content-Type: application/x-www-form-urlencoded can be received.
Explanation: data from form POST

Method 2: file_get_contents ("php: // input ");

Note:
Allows reading original POST data.
Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings.
Php: // input cannot be used for enctype = "multipart/form-data ".
Explanation:
For POST data without Content-Type specified, you can use file_get_contents ("php: // input"); to obtain the original data.
In fact, this method can be used to receive any POST data using PHP. You do not need to consider the Content-Type, including the binary file stream.
Therefore, Method 2 is the safest method.

Method 3, $ GLOBALS ['http _ RAW_POST_DATA '];

Note:
Always generate the $ HTTP_RAW_POST_DATA variable containing the original POST data.
This variable is generated only when data of the unrecognized MIME type is encountered.
$ HTTP_RAW_POST_DATA is unavailable for enctype = "multipart/form-data" form data
If the post data is not recognized by PHP, you can use $ GLOBALS ['http _ RAW_POST_DATA '] to receive the data,
For example, text/xml or soap
Explanation:
$ GLOBALS ['http _ RAW_POST_DATA '] stores the original POST data.
$ _ POST or $ _ REQUEST stores PHP formatted data in the form of key => value.
However, whether the POST data is saved in $ GLOBALS ['http _ RAW_POST_DATA '] depends on the centent-Type setting, that is, the Content-Type must be explicitly indicated when the POST data is used: application/x-www-form-urlencoded: POST data is stored in $ GLOBALS ['http _ RAW_POST_DATA '].

II. demonstration

1. how does PHP obtain XML data from POST and parse XML data?

For example, how do we process the data that the user replies when developing the enterprise number?
Document: http://qydev.weixin.qq.com/wiki/index.php? Title = % E6 % 8E % A5 % E6 % 94% B6 % E6 % 99% AE % E9 % 80% 9A % E6 % B6 % 88% E6 % 81% AF
First, check the document to know that after the development mode is enabled, when the user replies to the application, the server will POST a string of XML data to the verified callback URL.

Assuming the URL is a http://www.xxx.com
Http request method: POST
Http://www.xxx.com /? Msg_signature = ASDFQWEXZCVAQFASDFASDFSS × tamp = 13500001234 & nonce = 123412323

The XML content of POST is:

The code is as follows:



toUser
fromUser
1348831860
text
this is a test
1234567890123456
1

So how can I receive this content?
In this case, you can use method 2 (file_get_contents ("php: // input") and method 3 ($ GLOBALS ['http _ RAW_POST_DATA ']).

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

The code is as follows:


$ Input = file_get_contents ("php: // input"); // receives POST data
$ Xml = simplexml_load_string ($ input); // extract the POST data as a simplexml object
Var_dump ($ xml );

Method 3 ($ GLOBALS ['http _ RAW_POST_DATA '])

The code is as follows:


$ Input = $ GLOBALS ['http _ RAW_POST_DATA '];
Libxml_disable_entity_loader (true );
$ Xml = simplexml_load_string ($ input, 'simplexmlelement', LIBXML_NOCDATA );
Var_dump ($ xml );

This article provides a summary of several common methods for PHP to obtain POST data. here, I will share it with you. if you need it, refer to it. I ,...

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.