In contrast to $_POST
and $_GET
methods DELETE
, PUT
and others do not have pseudo-globais
variables that facilitate how to recover them.
Usually in API RESTful
, in methods PUT
and DELETE
we only work by passing the ID
of the resource that must be changed or deleted respectively from the server, so that the requests look something like this: / p>
Example with method PUT
PUT /123 HTTP/1.1
Host: exemplo.com.br
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
nome=teste&[email protected]
Example with method DELETE
DELETE /123 HTTP/1.1
Host: exemplo.com.br
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
With this we can get the resource that must be changed or deleted using $_SERVER['PATH_INFO']
, and the information can be obtained using the file_get_contents(php://input)
variable that takes the raw
(raw) submission of the request, being necessary to work converting the content raw material so it can be handled in an easier way.
An example of how it could be done, taken from here from Meta and adapted
$metodo = $_SERVER['REQUEST_METHOD'];
$recurso = explode("/", substr(@$_SERVER['PATH_INFO'], 1));
$conteudo = file_get_contents(php://input);
switch ($metodo ) {
case 'PUT':
funcao_para_put($recurso, $conteudo);
break;
case 'POST':
funcao_para_post($recurso, $conteudo);
break;
case 'GET':
funcao_para_get($recurso, $conteudo);
break;
case 'HEAD':
funcao_para_head($recurso, $conteudo);
break;
case 'DELETE':
funcao_para_delete($recurso, $conteudo);
break;
case 'OPTIONS':
funcao_para_options($recurso, $conteudo);
break;
default:
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
die('{"msg": "Método não encontrado."}');
break;
}
In the case of DELETE, we only need the resource ID, so it would not be necessary to get the raw
of the request, in the POST
, GET
you could choose to work using the same pseudo-global variables, PUT
using file_get_contents(php://input)
as well as @bigown responded.