PHP simulates the $_PUT implementation code

  • 2020-03-31 20:29:14
  • OfStack

PHP has $_GET, $_POST, but no $_PUT, so if you need to use it, you'll have to simulate it yourself:
 
$_PUT = array(); 
if ('PUT' == $_SERVER['REQUEST_METHOD']) { 
parse_str(file_get_contents('php://input'), $_PUT); 
} 

The data obtained from PHP ://input is raw data, so you need to parse it with parse_str.

But to be sure, when the form is enctype = "multipart/form - the data Type (that is, upload the file Type), when this method is invalid (at this point the PHP: / / input is empty), once found PHP content-type request is multipart/form - data, will be unconditional give you processing form data, and then save to $_FILES, at this time can't get the raw data, only some partial door way, apache, for example, Modify httpd.conf (to use the RequestHeader syntax, activate the header module first) :
 
<Location "/demo.php"> 
RequestHeader set Content-Type foobar 
</Location> 

By resetting the content-type header to foobar (as long as it is not multipart/form-data), then PHP ://input will have data, but the original $_FILES data will not exist, so basically only on the demonstration meaning, if you want to get raw data, you can only generate according to the data, in PEAR has a similar implementation: HTTP_Request2_MultipartBody.

Browsers are generally only allowed to use the GET/POST method, and while you can send the PUT method via JS, you still have to write code. In contrast, the CURL command under the command line is much more convenient and useful for developing tests, so it is necessary to learn:

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

This will send the id, title data through the PUT method, and the code for demo.php during the test will be similar to the above PHP ://input.

Note the always_populate_raw_post_data setting in php.ini.

Related articles: