Several ways to get post data from PHP

Source: Internet
Author: User
Tags cdata

A few ways to get post data from PHP

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: That is, the data that forms post

method 2, file_get_contents ("Php://input");
Description
Allows the raw data to be read from the POST.
Compared to $HTTP _raw_post_data, it brings less pressure to 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 can be used for any data that is received by the post in PHP. Regardless of the Content-type, including binary file streams is also possible.
So using method Two is the safest method.

method 3, $GLOBALS [' Http_raw_post_data '];
Description
Always produces $HTTP _raw_post_data variable contains the original POST data.
This variable is only generated when it encounters data that does not recognize the MIME type.
$HTTP _raw_post_data is not available for enctype= ' multipart/form-data ' form data
If the POST data is not recognized by PHP, it can be received $GLOBALS [' Http_raw_post_data '].
Like text/xml or SOAP, etc.
Explain:
The $GLOBALS [' Http_raw_post_data '] stores the raw data that came back from the POST.
$_post or $_request stores the data in PHP in the form of a key=>value format.
But whether or not the data in $globals[' Http_raw_post_data ' is saved depends on the settings of the Centent-type, that is, the post data must be explicitly indicated content-type:application/ X-www-form-urlencoded,post data will be stored in the $GLOBALS [' Http_raw_post_data ']

Second, the demonstration

1. How PHP obtains post XML data and parse XML data
For example, when we develop enterprise number, how to deal with the data that the user replies to come over?
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 look at the document, you know: when the development mode is enabled, when the user replies to the application information, the server will post a string of XML data to the validated callback URL

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

The XML content of the post is:

<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 get this part, huh?
This can be used: Method 2 (file_get_contents ("Php://input")), Method 3 ($GLOBALS [' Http_raw_post_data '])

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

$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 '])

$input = $GLOBALS [' Http_raw_post_data '];libxml_disable_entity_loader (true); $xml = simplexml_load_string ($input, ' SimpleXMLElement ', libxml_nocdata); Var_dump ($xml);


Document Related:
1. PHP Gets the post parameters in several ways:
http://lhdst-163-com.iteye.com/blog/1680297
2. The most primitive data method for PHP to get post
http://my.oschina.net/junn/blog/148277
3. Four common methods of post submission data
http://my.oschina.net/junn/blog/277254

Several ways to get post data from PHP

Related Article

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.