Unlike $_POST
and $_GET
the methods DELETE
, PUT
among others have no variables pseudo-globais
which make it easier to recover them.
Usually in a API RESTful
, in the methods PUT
and DELETE
we work only by passing the ID
of the resource that must be changed or deleted respectively from the server, so that the requests are more or less like this:
Example with the 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 the 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 we can get using the variable file_get_contents(php://input)
who picks up the shipment raw
(raw) of the request, it being necessary to work converting the raw content so that it can be worked in an easier way.
An example of how it could be done, taken from here of the 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 take the raw
of the request, in POST
, GET
you could choose to work using the same pseudo global variables, and on PUT
using the file_get_contents(php://input)
as @Maniero replied.
I gave a corrected, I ate ball to respond fast and without reading haha, thanks for the touch :)
– MarceloBoni