Identify and get PUT and DELETE variables

6

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 GET and POST I use:

$_POST;
$_GET;

When executing a request for an API as PUT or DELETE , as in API do I get these values? I hope it was clear, and that they can help me, I did not post more code because the doubt is quite specific.

    
asked by anonymous 17.12.2016 / 21:03

2 answers

9

To make it easy to create similar variables for your application's consumption. 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 load 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']
    
17.12.2016 / 21:30
6

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.

    
17.12.2016 / 21:26