Identify and retrieve PUT and DELETE variables

Asked

Viewed 4,162 times

7

I’m developing a solution for API, I can currently identify the method by:

$_SERVER['REQUEST_METHOD']

How can I get the variables PUT and DELETE? As in the GET and POST i use:

$_POST;
$_GET;

When making a request for a API as PUT or DELETE, as in the API do I capture these values? I hope that it has been clear, and that you can help me, I have not posted more code because the doubt is quite specific.

2 answers

13


To facilitate you can create similar variables for consumption of your application. The only difference is that you will have to call some function to load them, it will not be automatic.

You’ll have to put this in a file and upload it somehow, probably with include.

global $_DELETE = array();
global $_PUT = array();

if (!strcasecmp($_SERVER['REQUEST_METHOD'], 'DELETE')) {
    parse_str(file_get_contents('php://input'), $_DELETE);
}
if (!strcasecmp($_SERVER['REQUEST_METHOD'], 'PUT')) {
    parse_str(file_get_contents('php://input'), $_PUT);
}

Then you can use:

include_once('newVerbs.php')
echo $_PUT['email']

I put in the Github for future reference.

8

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.

  • 1

    I gave a corrected, I ate ball to respond fast and without reading haha, thanks for the touch :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.