Several ways to get post data from PHP summary _php tips

Source: Internet
Author: User
Tags cdata form post http request

One, PHP to obtain post data several methods

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: The data from the form post

Method 2, File_get_contents ("Php://input");

Description
Allows you to read the original data for the POST.
Compared with $HTTP _raw_post_data, it brings less pressure on memory and does not require any special php.ini settings.
Php://input cannot be used for enctype= "Multipart/form-data".
Explain:
For post data that does not specify Content-type, you can use File_get_contents ("Php://input") to get the raw data.
In fact, this method is available for any data that receives a post in PHP. Instead of considering Content-type, including binary file streams, you can also.
So using method Two is the most insurance method.

Method 3, $GLOBALS [' Http_raw_post_data '];

Description
Always produces $HTTP the _raw_post_data variable contains the original POST data.
This variable is generated only when data with an unrecognized MIME type is encountered.
$HTTP _raw_post_data is not available for enctype= "multipart/form-data" form data
If the POST data is not available in PHP, you can use the $GLOBALS [' Http_raw_post_data '] to receive,
Like text/xml or SOAP, etc.
Explain:
$GLOBALS [' Http_raw_post_data '] stores the original data that comes back.
$_post or $_request stores the data that PHP formats later in key=>value format.
However, the data that is posted in $globals[' Http_raw_post_data ' depends on the settings of the Centent-type, which means that the post data must be explicitly indicated content-type:application/ X-www-form-urlencoded,post data is stored in the $GLOBALS [' Http_raw_post_data ']

Second, demo

1. How does PHP get post XML data and parse XML data

For example, when we develop the micro-credit enterprise number, how to deal with the user back to the data?
Documents: 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
Look at the documentation first, and know that after you enable development mode, when the user responds to the application, the micro-service will post a string of XML data to the validated callback URL

Suppose the URL is http://www.xxx.com
HTTP request mode: POST
http://www.xxx.com/?msg_signature=ASDFQWEXZCVAQFASDFASDFSS&timestamp=13500001234&nonce=123412323

The XML content of the post is:

Copy Code code as follows:

<xml>
<tousername><! [cdata[touser]]></tousername>
<fromusername><! [cdata[fromuser]]></fromusername>
<CreateTime>1348831860</CreateTime>
<msgtype><! [cdata[text]]></msgtype>
<content><! [Cdata[this is a test]]></content>
<MsgId>1234567890123456</MsgId>
<AgentID>1</AgentID>
</xml>

So how do you receive this, eh?
This can be used at this point: Method 2 (file_get_contents ("Php://input")), Method 3 ($GLOBALS [' Http_raw_post_data '])

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

Copy Code code as follows:

$input = file_get_contents ("Php://input"); Receive post data
$xml = simplexml_load_string ($input); Extract post data as SimpleXML object
Var_dump ($xml);

Method 3 ($GLOBALS [' Http_raw_post_data '])

Copy Code code as follows:

$input = $GLOBALS [' Http_raw_post_data '];
Libxml_disable_entity_loader (TRUE);
$xml = simplexml_load_string ($input, ' simplexmlelement ', libxml_nocdata);
Var_dump ($xml);

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.