如何在PHP中获取PUT/DELETE参数

dev*_*234 6 php httprequest

我正在编写REST Web服务,并想知道如何处理put或delete参数是PHP.

我正在接受如下输入,

$input = file_get_contents('php://input');
echo $input;
Run Code Online (Sandbox Code Playgroud)

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

我怎样才能访问这些变量

echo $_POST['imei'];
echo $_POST['email'];
echo $_GET['imei'];
echo $_GET['email'];
Run Code Online (Sandbox Code Playgroud)

我发现在PHP中没有像$ _PUT或$ _DELETE来处理输入参数.得到这个的方法是什么?

Kri*_*h R 11

你可以试试这个,PHP没有内置的方法来做到这一点,可以从传入的流中读取到PHP , php://input.

 parse_str(file_get_contents("php://input"));
Run Code Online (Sandbox Code Playgroud)

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";
}
Run Code Online (Sandbox Code Playgroud)

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