我正在编写一个REST Web服务,想知道如何处理put或delete参数是PHP。
我将输入如下,
$input = file_get_contents('php://input');
echo $input;
输出=imei=1234567890&电子邮件=你好%40gmail1.com
我如何访问这些变量,如
echo $_POST['imei'];
echo $_POST['email'];
echo $_GET['imei'];
echo $_GET['email'];
我发现php中没有类似$_PUT或$_DELETE的东西来处理输入参数。得到这个的方法是什么?
你能试试吗,PHP没有内置的方法来做到这一点,可以从传入流读取到PHP,php://input
。
parse_str(file_get_contents("php://input"),$post_vars);
例如:
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";
}
参考编号:http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php
遗憾的是,php默认情况下不处理put或delete请求。我创建了一个库来处理这种请求。它还处理多部分请求(PUT PATCH DELETE等)
你可以在这里找到https://github.com/notihnio/php-request-parser