检测 PHP 中的请求类型(GET、POST、PUT 或 DELETE)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/359047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting request type in PHP (GET, POST, PUT or DELETE)
提问by UnkwnTech
How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?
如何检测在 PHP 中使用了哪种请求类型(GET、POST、PUT 或 DELETE)?
回答by gnud
By using
通过使用
$_SERVER['REQUEST_METHOD']
Example
例子
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// The request is using the POST method
}
For more details please see the documentation for the $_SERVER variable.
有关更多详细信息,请参阅$_SERVER 变量的文档。
回答by neu242
REST in PHP can be done pretty simple. Create http://example.com/test.php(outlined below). Use this for REST calls, e.g. http://example.com/test.php/testing/123/hello. This works with Apache and Lighttpd out of the box, and no rewrite rules are needed.
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;
}
回答by Peter
Detecting the HTTP method or so called REQUEST METHODcan be done using the following code snippet.
REQUEST METHOD可以使用以下代码片段检测 HTTP 方法或所谓的方法。
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'POST'){
// Method is POST
} elseif ($method == 'GET'){
// Method is GET
} elseif ($method == 'PUT'){
// Method is PUT
} elseif ($method == 'DELETE'){
// Method is DELETE
} else {
// Method unknown
}
You could also do it using a switchif you prefer this over the if-elsestatement.
switch如果您更喜欢这个而不是if-else声明,您也可以使用 a 来做到这一点。
If a method other than GETor POSTis required in an HTML form, this is often solved using a hidden field in the form.
如果HTML 表单中不需要GET或POST需要其他方法,通常可以使用表单中的隐藏字段来解决。
<!-- DELETE method -->
<form action='' method='POST'>
<input type="hidden" name'_METHOD' value="DELETE">
</form>
<!-- PUT method -->
<form action='' method='POST'>
<input type="hidden" name'_METHOD' value="PUT">
</form>
For more information regarding HTTP methods I would like to refer to the following StackOverflow question:
有关 HTTP 方法的更多信息,我想参考以下 StackOverflow 问题:
回答by HelpNeeder
We can also use the input_filterto detect the request method while also providing security through input sanitation.
我们还可以使用input_filter来检测请求方法,同时还可以通过输入卫生提供安全性。
$request = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);
回答by Artegon
You can use getenvfunction and don't have to work with a $_SERVERvariable:
您可以使用getenv函数而不必使用$_SERVER变量:
getenv('REQUEST_METHOD');
More info:
更多信息:
回答by nurettin
Since this is about REST, just getting the request method from the server is not enough. You also need to receive RESTful route parameters. The reason for separating RESTful parameters and GET/POST/PUT parameters is that a resource needs to have its own unique URL for identification.
由于这是关于 REST 的,仅从服务器获取请求方法是不够的。您还需要接收 RESTful 路由参数。将 RESTful 参数和 GET/POST/PUT 参数分开的原因是资源需要有自己唯一的 URL 进行标识。
Here's one way of implementing RESTful routes in PHP using Slim:
这是使用 Slim 在 PHP 中实现 RESTful 路由的一种方法:
https://github.com/codeguy/Slim
https://github.com/codeguy/Slim
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
And configure the server accordingly.
并相应地配置服务器。
Here's another example using AltoRouter:
这是使用 AltoRouter 的另一个示例:
https://github.com/dannyvankooten/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');
回答by Juned Ansari
It is Very Simple just use $_SERVER['REQUEST_METHOD'];
非常简单,只需使用$_SERVER['REQUEST_METHOD'];
Example:
例子:
<?php
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
//Here Handle GET Request
break;
case 'POST':
//Here Handle POST Request
break;
case 'DELETE':
//Here Handle DELETE Request
break;
case 'PUT':
//Here Handle PUT Request
break;
}
?>
回答by Amit Patange
$request = new \Zend\Http\PhpEnvironment\Request();
$httpMethod = $request->getMethod();
In this way you can also achieve in zend framework 2 also. Thanks.
通过这种方式,您也可以在 zend 框架 2 中实现。谢谢。
回答by Shaan Ansari
In core php you can do like this :
在核心 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;
}
?>
回答by Karol Sobański
It is valuable to additionally note, that PHP will populate all the $_GETparameters even when you send a proper request of other type.
值得额外注意的是,$_GET即使您发送其他类型的正确请求,PHP 也会填充所有参数。
Methods in above replies are completely correct, however if you want to additionaly check for GETparameters while handling POST, DELETE, PUT, etc. request, you need to check the size of $_GETarray.
在上述答复的方法是完全正确的,但是如果你想additionaly检查GET参数,同时处理POST,DELETE,PUT等的要求,你需要检查的大小$_GET排列。

