As we all know, in PHP we can get GET and POST parameters with their respective global variables ( $_GET
and $_POST
), but for the case of PUT , DELETE , PATCH and maybe other HTTP methods, we see that PHP doesn't handle it for us.
I've tried the only solution I've found on all sites/forums and I don't get the result I'm supposed to get:
1st Test:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
parse_str(file_get_contents("php://input"),$put_vars);
echo $put_vars['name'];
}
Result:
Notice: Undefined index: name in C:\...\UpdateController.php on line 9
2nd Test:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
parse_str(file_get_contents("php://input"),$put_vars);
var_dump($put_vars);
}
Result:
array(1) {
["----------------------------531981617569841925398802
Content-Disposition:_form-data;_name"]=>
string(89) ""name"
Mi nombre y apellidos
----------------------------531981617569841925398802--
"
}
Finally, the problem was different, but basically, for those who do not know how to obtain the data from the PUT, DELETE, PATCH, etc. requests, this solution is valid.
For those who have not worked that solution, like me, it turns out that it is because at the time of sending the request, I have sent the data in
multipart/form-data
(as Marcos has said in his comment ).In my case I have been able to modify the "format"
x-www-form-urlencoded
in the Postman options very easily, and it works perfectly for me: