12

i am writing a REST webservice and would like to know how can i handle put or delete argument is PHP.

i am taking the inputs as following,

$input = file_get_contents('php://input');
echo $input;

output = imei=1234567890&email=hello%40gmail1.com

how can i access these variables like

echo $_POST['imei'];
echo $_POST['email'];
echo $_GET['imei'];
echo $_GET['email'];

i found there is nothing like $_PUT or $_DELETE in php to handle the input params. what is the way to get this ?

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
dev1234
  • 5,376
  • 15
  • 56
  • 115

2 Answers2

14

Can you try this, PHP doesn't have a built-in way to do this, can be read from the incoming stream to PHP, php://input.

 parse_str(file_get_contents("php://input"),$post_vars);

EX:

  if($_SERVER['REQUEST_METHOD'] == 'GET') {
    echo "this is a get request\n";
    echo $_GET['fruit']." is the fruit\n";
    echo "I want ".$_GET['quantity']." of them\n\n";
} elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
    echo "this is a put request\n";
    parse_str(file_get_contents("php://input"),$post_vars);
    echo $post_vars['fruit']." is the fruit\n";
    echo "I want ".$post_vars['quantity']." of them\n\n";
}

Ref: http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php

ruleboy21
  • 5,510
  • 4
  • 17
  • 34
Krish R
  • 22,583
  • 7
  • 50
  • 59
1

Unfortunately php does not handle put or delete requests by default. I have created a library in order to handle this kind of requests. It also handles multipart requests (PUT PATCH DELETE etc)

You can find it here https://github.com/notihnio/php-request-parser

  • 2
    Whilst we love links in answers, we tend to discourage link-only answers as they are rendered useless when the link expires. Please update your answer with more information :) – I.T Delinquent Jul 01 '19 at 15:39