Detecting request type in PHP (GET, POST, PUT or DELETE)

前端 未结 13 1696
不思量自难忘°
不思量自难忘° 2020-11-22 08:02

How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?

13条回答
  •  借酒劲吻你
    2020-11-22 08:28

    I used this code. It should work.

    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;
    }
    

    This above code will work with REST calls and will also work with html form

提交回复
热议问题