We who develop the Restful interface of PHP will all know that there is a PUT method to update resources, so how to use the PUT HTTP method to update resources? This article mainly shares with you the detailed explanation of parameter submission and reception of the Restful PUT method in PHP. I hope it can help everyone. .
Sometimes we find that the parameters sent by the PUT method are not of the type we want. We want a parameter array, but it is received as a string. This kind of The problem is actually that we got the type of Content-Type
wrong. What we sent was application/json
or application/x-www-form-urlencoded
corresponding to data, but Content-Type
is multipart/form-data
, so the received data becomes form-data:
----------------------------217287928126218120101488Content-Disposition: form-data; name="status" 1 ----------------------------217287928126218120101488--
and Restful The data is basically transmitted in json format, so the HTTP header sent should be Content-Type=application/json
.
Use POST MAN:
PUT method accepts parameters use:
parse_str(file_get_contents('php://input'), $data);
Parameter saving In $data
.
If you use the tp5 framework, you can use the helper function:
input('put.status');input('put.');
ps: The form-data misunderstanding here should be parsed internally by PHP's POST and PUT methods multipart/form-data
Due to the different data methods, the POST method parses the parameters into $_POST and leaves the content blank, while PUT does not do this step, so pay attention to the difference in usage.
Related recommendations:
PHP’s automatic identification of the content type returned by Restful
The above is the detailed content of Detailed explanation of parameter submission and reception of Restful PUT method in PHP. For more information, please follow other related articles on the PHP Chinese website!