Php: // input & $ _ POST & $ HTTP_RAW_POST_DATA

Source: Internet
Author: User
Php: input & $ _ POST & $ HTTP_RAW_POST_DATA details PHP APIs for developing webservice do not necessarily communicate with clients in data formats that can be processed by PHP, for example, json xml commonly used in APIs, in this scenario, we need to use other methods to obtain the data sent from the client and then process it, next we will simulate the data interaction mode in the php api scenario when the client requests.

At the same time, we must understand that a get request does not carry form data. the essence of a get request is to request a server resource. The data sent to the server is in the Query String/Query Path in the request header, the request header is a uri. Note that the meaning of the get request is to get resources from the server rather than sending "data" to the server ". A post request contains Form Data, that is, Form Data. it is intended for the server to send "Data ".

Get post

It can be seen that the get parameters are all attached to the request header, not like the attribute values in the content-length/content-type/from data in the post that describe the data content carried by the request.

Data types supported by php

The data format supported by PHP (note: the supported data here does not mean that it can be obtained simply, meaning that it can be automatically processed and automatically filled in $ _ POST/$ _ FILES, once again, the get request does not send "data" to the server. it is a fetch, not a storage, so $ _ GET is a parameter rather than data)

Application/x-www-form-urlencoded common form data

Multipart/form-data file upload form enctype = "multipart/form-data"

Data formats not supported by PHP

Application/json

Application/xml


It cannot be processed but can be obtained. you can manually process it by referring to the table



Application/x-www-form-urlencoded Multipart/form-data Application/json Application/xml
Php: // input Available Not available Available Available
$ _ POST Available Available Not available Not available
$ HTTP_RAW_POST_DATA Available Not available Available Available


PS: $ HTTP_RAW_POST_DATA has been deprecated by PHP7 because php: // input fully covers all its functions, and the pressure on the server is much greater than php: // input, the weakest thing is that it uses the default configuration to give way $ _ POST. if $ _ POST can process data, it has to depend on the site. of course, you can configure php. ini opens the always_populate_raw_post_data configuration item, it will always fill in the data it can recognize, even if $ _ POST can be processed.


PHP Data processing method

Php: // input:Except for the unrecognized data type of multipart/form-data, other formats can be obtained, and then we can process the data according to the data type.

$ _ POST:The application/x-www-form-urlencoded multipart/form-data format can be recognized, and the json xml format cannot be recognized.

$ HTTP_RAW_POST_DATA:The recognition capability is the same as php: // input. it is mutually exclusive with $ _ POST in the default configuration and has a priority lower than $ _ POST.


Instance

First paste the server code

Note: When the sent data is multipart/form-data, PHP will store the file-type data in $ _ FILES and the text in $ _ POST, but they are in the same group. they are all data in from data, which is different from $ _ GET. $ _ GET is filled with the Query String parameter in the request header.

 

1. send application/x-www-form-urlencoded standard data

We can see that php: // input and $ _ POST/$ _ FILES can recognize and receive data of this type. In fact, $ _ HTTP_RAW_POST_DATA can also be accepted, however, by default, it is mutually exclusive with $ _ POST/$ _ FILES and has a low priority. Therefore, when $ _ POST/$ _ FILES contains data, it will not be filled, you can modify the configuration so that he can always fill in his identifiable data.

2. send multipart/form-data

Php: // the input and $ HTTP_RAW_POST_DATA values are empty, indicating that the two cannot identify the data streams of such rows (in fact, they only cannot recognize such rows, and there is no problem with others ), $ _ POST/$ _ FILES can be recognized normally

3. send application/json format

Data body:

Php: // input and $ HTTP_RAW_POST_DATA can be identified, and $ _ POST cannot be identified.

4. send application/xml format

Data body:

Php: // input and $ HTTP_RAW_POST_DATA can be identified, and $ _ POST cannot be identified.

Summary

Of course there are many other data types that we will not test. you can do it yourself.

Application/x-www-form-urlencoded is the standard data stream type of PHP, which can be identified by three

Multipart/form-data can only be identified by $ _ POST/$ _ FILES. The other two cannot be identified.

Application/json application/xml and so on can only be identified by php: // input and $ HTTP_RAW_POST_DATA. this is also the common use of php when we develop interfaces: // input data input stream to obtain the data carried by the client request

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.