PHP 检测请求类型(GET、POST、PUT 或 DELETE),而不使用 $_SERVER['REQUEST


PHP detecting request type (GET, POST, PUT or DELETE) without using $_SERVER['REQUEST_METHOD']

如何在不使用操作页面$_SERVER['REQUEST_METHOD']的情况下找到服务器请求类型(GET, POST, PUT or DELETE)

我正在提交来自abc.php的页面表单操作页面是操作页面。

我需要打印使用的方法

常规 if 语句

if(!empty($_GET)) { 
    $request = (!empty($_POST)) ? 'both get and post' : 'get';
} else if(!empty($_POST)) {
    $request = 'post';        
}
//... You get the picture

编辑:我在获取检查中添加了一个三元,以解决Gumbo在评论中指出的问题。您可以同时提供 GET 和 POST vars,因为您可以使用 get 参数将数据发布到 url,即/forms/addFileToCompany/?companyId=23

而现在因为我是一个彻头彻尾的污秽,是你见过的最可怕的三元!请注意,这只是为了好玩,我真的不建议使用它。

$request = (!empty($_GET)) 
    ? (!empty($_POST)) 
        ? 'both post and get' 
        : 'get'
    : (!empty($_POST))
        ? 'post'
        : (/* Keep it going for whatever */ );
我相信有一种

棘手的方法和一种不那么聪明的方法。是手动检查它,例如:

if( isset($_GET) ) $request_type = 'GET Method'; 
elseif( isset($_POST) ) $request_type = 'POST Method';