Example _php instance in PHP to simulate processing HTTP PUT requests

Source: Internet
Author: User

For more information on HTTP put, see this article: Http://www.jb51.net/article/52515.htm.

PHP has $_get,$_post, but there is no $_put, so if you need to use it, you have to simulate yourself:

Copy Code code as follows:

$_put = Array ();
if (' put ' = = $_server[' Request_method ']) {
Parse_str (file_get_contents (' Php://input '), $_put);
}

The data obtained through Php://input is raw, so it needs to be parsed with PARSE_STR.

It should be explained, however, that when the form is enctype= the "Multipart/form-data" type (that is, the type of upload file), this method is invalid (at this time php://input is empty). Once the PHP discovery request Content-type is multipart/form-data, will unconditionally processing the form data for you, then saves in the $_files, at this time cannot obtain raw data, only uses some partial gate method

Take Apache, for example, to modify httpd.conf (to use the Requestheader syntax, activate the header module first):

Copy Code code as follows:

<location "/demo.php" >
Requestheader Set Content-type Foobar
</Location>

By resetting the Content-type request header for Foobar (as long as it is not multipart/form-data), Php://input has data at this time, but the original due $_files data does not exist, so basically only the meaning of the demo, If you want to get raw data, you can only generate them from the data, in pear there is a similar implementation: Http_request2_multipartbody.

Browsers generally only allow the use of the Get/post method, although you can send the Put method through JS, but also have to write code, relatively, the use of command line of the Curl command is very convenient, in the development of testing is very useful, so learn it is necessary:

Copy Code code as follows:

Curl-x put http://www.domain.com/demo.php-d "id=1"-D "title=a"

This will send the ID through the Put method, the title data, and the demo.php code at the time of the test is similar to the php://input above.

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.