我怎么能检测到哪个请求类型被使用(GET, POST, PUT或删除)在PHP?
当前回答
您可以获得任何查询字符串数据,例如www.example.com?id=2&name=r
您必须使用$_GET['id']或$_REQUEST['id']获取数据。
Post数据意味着像form <form action= " method=' Post '>你必须使用$_POST或$_REQUEST。
其他回答
因为这是关于REST的,所以仅仅从服务器获取请求方法是不够的。您还需要接收RESTful路由参数。分离RESTful参数和GET/POST/PUT参数的原因是资源需要有自己的唯一URL用于标识。
下面是使用Slim在PHP中实现RESTful路由的一种方法:
https://github.com/codeguy/Slim
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
并相应地配置服务器。
下面是另一个使用AltoRouter的例子:
https://github.com/dannyvankooten/AltoRouter
$router = new AltoRouter();
$router->setBasePath('/AltoRouter'); // (optional) the subdir AltoRouter lives in
// mapping routes
$router->map('GET|POST','/', 'home#index', 'home');
$router->map('GET','/users', array('c' => 'UserController', 'a' => 'ListAction'));
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
我使用了这个代码。它应该会起作用。
function get_request_method() {
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
if($request_method != 'get' && $request_method != 'post') {
return $request_method;
}
if($request_method == 'post' && isset($_POST['_method'])) {
return strtolower($_POST['_method']);
}
return $request_method;
}
上面的代码将与REST调用一起工作,也将与html表单一起工作
<form method="post">
<input name="_method" type="hidden" value="delete" />
<input type="submit" value="Submit">
</form>
PHP中的REST可以非常简单地实现。创建http://example.com/test.php(如下所示)。将此用于REST调用,例如http://example.com/test.php/testing/123/hello。这与Apache和Lighttpd一起开箱即用,不需要重写规则。
<?php
$method = $_SERVER['REQUEST_METHOD'];
$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));
switch ($method) {
case 'PUT':
do_something_with_put($request);
break;
case 'POST':
do_something_with_post($request);
break;
case 'GET':
do_something_with_get($request);
break;
default:
handle_error($request);
break;
}
在core php中,你可以这样做:
<?php
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
//Here Handle GET Request
echo 'You are using '.$method.' Method';
break;
case 'POST':
//Here Handle POST Request
echo 'You are using '.$method.' Method';
break;
case 'PUT':
//Here Handle PUT Request
echo 'You are using '.$method.' Method';
break;
case 'PATCH':
//Here Handle PATCH Request
echo 'You are using '.$method.' Method';
break;
case 'DELETE':
//Here Handle DELETE Request
echo 'You are using '.$method.' Method';
break;
case 'COPY':
//Here Handle COPY Request
echo 'You are using '.$method.' Method';
break;
case 'OPTIONS':
//Here Handle OPTIONS Request
echo 'You are using '.$method.' Method';
break;
case 'LINK':
//Here Handle LINK Request
echo 'You are using '.$method.' Method';
break;
case 'UNLINK':
//Here Handle UNLINK Request
echo 'You are using '.$method.' Method';
break;
case 'PURGE':
//Here Handle PURGE Request
echo 'You are using '.$method.' Method';
break;
case 'LOCK':
//Here Handle LOCK Request
echo 'You are using '.$method.' Method';
break;
case 'UNLOCK':
//Here Handle UNLOCK Request
echo 'You are using '.$method.' Method';
break;
case 'PROPFIND':
//Here Handle PROPFIND Request
echo 'You are using '.$method.' Method';
break;
case 'VIEW':
//Here Handle VIEW Request
echo 'You are using '.$method.' Method';
break;
Default:
echo 'You are using '.$method.' Method';
break;
}
?>
博士TL;
true的“原生源”是$_SERVER全局变量。请求方法 保存在“REQUEST_METHOD”键下。
检查$_SERVER数组
$ _SERVER(“REQUEST_METHOD”)
PHP方法
但是您可以使用许多变通方法来获得方法。像filter_input: filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);
填词 或者使用外部库,比如:
Symfony \ HttpFoundation \ \组件请求 \ Zend \ \ PhpEnvironment \ Http请求
结论
但最简单的方法是使用:$_SERVER['REQUEST_METHOD']。
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 如何实现一个好的脏话过滤器?
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PHP中获得变量名作为字符串?
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 什么是HTTP中的“406-不可接受的响应”?
- 在PHP中对动态变量名使用大括号
- 最好的轻量级web服务器(只有静态内容)的Windows