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×tamp=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);