PHP simulation $_put Implementation Code _php tutorial

Source: Internet
Author: User
PHP has $_get,$_post, but no $_put, so if you need to use it, then you have to simulate it yourself:
Copy CodeThe 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, so we need to parse it with PARSE_STR.

However, it is necessary to note that when the form is enctype= "Multipart/form-data" type (that is, the type of upload file), this method is invalid (this time the Php://input is empty), Once the PHP discovery request Content-type is multipart/form-data, will be unconditional for you to process the form data, and then save to $_files, at this time can not get raw data, can only use some partial gate method, to Apache, for example, Modify httpd.conf (to use the Requestheader syntax, first activate the header module):
Copy CodeThe 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), at this time the Php://input has data, but the original $_files data does not exist, so basically only the meaning of the demonstration, If you want to get raw data, you can only build on your own data, there are similar implementations in pear: 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 to write code, relatively speaking, the use of the command Line Curl command is much more convenient, in the development of testing is very useful, so learning is still necessary:

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

This will send the ID by the Put method, the title data, the test demo.php code is similar to the above php://input, do not elaborate.

Add: Note the Always_populate_raw_post_data settings in php.ini.

http://www.bkjia.com/PHPjc/321517.html www.bkjia.com true http://www.bkjia.com/PHPjc/321517.html techarticle PHP has $_get,$_post, but there is no $_put, so if you need to use it, you have to simulate it yourself: Copy the code 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.