We develop the RESTful interface of PHP will know there is put method update resources, that is how to use put this HTTP method to update resources, this article mainly share with you in PHP the RESTful put method parameter submission and receive detailed, hope to help everyone.
Send a PUT request
Sometimes we find that the parameter received by the Put method is not the type we want, we want a parameter array, but we receive a string, the problem is we put Content-Type
the type wrong, we send application/json
or the application/x-www-form-urlencoded
corresponding data, but it Content-Type
is multipart/form-data
, The data that is received becomes form-data:
----------------------------217287928126218120101488content-disposition:form-data; Name= "status" 1----------------------------217287928126218120101488--
and restful data is basically transmitted in JSON format, so the HTTP headers sent should be Content-Type=application/json
.
Using Post man:
Receive put request parameters
The Put method accepts parameters using:
Parse_str (file_get_contents (' Php://input '), $data);
The parameters are saved in $data
.
If you use the TP5 framework, you can use helper functions to implement:
Input (' put.status '); input (' Put. ');
PS: Here form-data misunderstanding should be caused by the PHP post and put method internal parsing multipart/form-data
data, the Post method to parse the parameters to $_post and the content contents empty, and put does not have this step, so pay attention to the use of the difference.