Overwriting $_POST for PUT or DELETE requests

前端 未结 4 929
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 23:11

In PHP I would like to be able to access PUT and DELETE vars globally similar to how GET and POST vars are accessed globa

4条回答
  •  孤街浪徒
    2020-12-15 23:32

    Leave Post and Get as it is. it shouldn't be modified as it's for reading purposes only. Create $_PUT and $_DELETE globals:

    //  globals
    $_DELETE = array ();
    $_PUT = array ();
    
    switch ( $_SERVER['REQUEST_METHOD'] ) {
        case !strcasecmp($_SERVER['REQUEST_METHOD'],'DELETE'):
            parse_str( file_get_contents( 'php://input' ), $_DELETE );
            break;
    
        case !strcasecmp($_SERVER['REQUEST_METHOD'],'PUT'):
            parse_str( file_get_contents( 'php://input' ), $_PUT );
            break;
    }
    

    Not tested but you should get the idea. I was in the search for a rest framework myself some weeks ago and decided to go with python. Recess (http://www.recessframework.org/) sounds promising though

提交回复
热议问题