PHP simulation $ _ PUT implementation code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP simulates the $ _ PUT implementation code. PHP has $ _ GET, $ _ POST, but no $ _ PUT, so if you need to use it, you have to simulate it yourself: Copy the code as follows: $ _ PUTarray (); if (PUT $ _ ser php has $ _ GET, $ _ POST, but no $ _ PUT, so if you need to use it, you have to simulate it yourself:

The code is 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 data, so you need to use parse_str for parsing.

However, when the form is of the enctype = "multipart/form-data" type (that is, the type of the file to be uploaded), this method is invalid (php: // input is empty). once PHP finds that the request Content-Type is multipart/form-data, it processes the form data unconditionally and saves it to $ _ FILES, in this case, raw data cannot be obtained. you can only use some partial methods. take apache as an example to modify httpd. conf (to use the RequestHeader syntax, activate the header module first ):

The code is as follows:



RequestHeader set Content-Type foobar


By resetting the Content-Type request header to foobar (as long as it is not multipart/form-data), php: // input has data, however, the original $ _ FILES data does not exist, so it basically only indicates the meaning of the demo. if you want to get raw data, you can only generate it based on the data. PEAR has a similar implementation: HTTP_Request2_MultipartBody.

The browser generally only allows the GET/POST method. although The PUT method can be sent through JS, the code has to be written. it is much more convenient to use the CURL Command under the command line, it is useful in development and testing, so it is necessary to learn:

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

In this way, the id and title data will be sent through the PUT method. during the test, the demo. php code is similar to the above php: // input.

Note: The always_populate_raw_post_data settings in php. ini.

The example code is as follows: $ _ PUT = array (); if ('put' = $ _ SER...

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.