PHP post receive raw data instance in detail

Source: Internet
Author: User
Tags form post http post
This article is mainly to share with you in the PHP post receive the original data instance detailed, hoped can help everybody.

Typically, users use the browser Web page table to submit data to a one-way server post, and we use PHP to receive user post-to-server data and handle it appropriately. However, in some cases, if the user uses the client software to send the post data to the server PHP program, but not to use $_post to identify, then how to deal with it?

$_post Way to receive data

The $_post method is an array of variables passed through the HTTP POST method, which is an automatic global variable. If you use $_post[' name '] You can receive the Web form and the Web page asynchronously POST data, that is, $_post can only receive document type content-type:application/ X-www-form-urlencoded the submitted data.

$GLOBALS [' Http_raw_post_data '] way to receive data

If the data used for post is not a document type that PHP can recognize, such as text/xml or soap, we can use $globals[' http_raw_post_data ' to receive it. $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. This means that using $http_raw_post_data cannot receive data from a Web Form POST.

Php://input Way to receive data

If the better way to access the original POST data is php://input. Php://input 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, and php://input cannot be used for enctype= "Multipart/form-data".

For example, the user posts a file to the server using a client application, the contents of the file we don't care about, but we want to keep this file intact on the server, we can use the following code:


$input = file_get_contents (' php://input '); File_put_contents ($original, $input); $original as a file on the server

The above code uses file_get_contents (' Php://input ') to receive the post data, and then writes the data to the $original file, which can be understood as uploading a file from the client to the server, a lot of such applications, In particular, we will use PHP development in conjunction with the development of c,c++ and other applications for product development.

Related recommendations:

PHP solves the problem of post-large data loss

Parsing of Curl get POST request in PHP

Show how exceptions are handled when post submits data

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.