Home  >  Article  >  Backend Development  >  PHP simulation $_PUT implementation code_PHP tutorial

PHP simulation $_PUT implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:39:47711browse

There are $_GET and $_POST in PHP, but there is no $_PUT, so if you need to use it, you have to simulate it 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 data, so it needs to be parsed with parse_str.

However, it should be noted that when the form is of enctype="multipart/form-data" type (that is, the type of uploaded file), this method is invalid (at this time php://input is empty), once PHP discovers that the requested Content-Type is multipart/form-data, it will unconditionally process the form data on your behalf, and then save it to $_FILES. At this time, raw data cannot be obtained, and some partial methods can only be used. , taking apache as an example, modify httpd.conf (in order to use RequestHeader syntax, please activate the header module first):
Copy code The code is as follows:


RequestHeader set Content-Type foobar


By resetting the Content-Type request header For foobar (as long as it is not multipart/form-data), there is data in php://input at this time, but the original $_FILES data does not exist, so it is basically only of demonstration significance. If you want to get Raw data can only be generated by yourself based on the data. There is a similar implementation in PEAR: HTTP_Request2_MultipartBody.

Browsers generally only allow the use of GET/POST methods. Although you can send the PUT method through JS, you still have to write code. Relatively speaking, using the CURL command under the command line is much more convenient. During development It is very useful during testing, so it is necessary to learn it:

curl -X PUT http://www.domain.com/demo.php -d "id=1" -d "title=a"

In this way, the id and title data will be sent through the PUT method. The code of demo.php during testing is similar to the php://input above, so I won’t go into details.

Additional: Pay attention to the always_populate_raw_post_data setting in php.ini.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321517.htmlTechArticlePHP has $_GET, $_POST, but no $_PUT, so if you need to use it, you must not Don't simulate it yourself: Copy the code as follows: $_PUT = array(); if ('PUT' == $_SER...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn