CodeIgniter3 獲取GET/POST/PUT/DELETE方法多種方式傳參

丁丁丁夢濤發表於2020-12-04

CodeIgniter3 獲取GET/POST/PUT/DELETE方法多種方式傳參

if(!function_exists('deal_request_content_type')){
    /**
     * 方法 deal_request_content_type,根據是否上傳檔案處理請求引數結果
     * @return mixed 返回處理好的引數陣列
     */
    function deal_request_content_type(){
        $CI = &get_instance();
        if (in_array($_SERVER['REQUEST_METHOD'], ['POST', 'PUT']) 
            && in_array($_SERVER['CONTENT_TYPE'], ['application/x-www-form-urlencoded', 'multipart/form-data'])
        ) {
            $params = $CI->input->post();
        } else if(in_array($_SERVER['REQUEST_METHOD'], ['GET', 'DELETE'])) {
            $params = $CI->input->get();
        } else {
            $params = json_decode($CI->input->raw_input_stream, TRUE);
            if(empty($params)){
                $params = [];
            }
        }
        // dd($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE'], $CI->input->post(), $params);
        return fields_filter_trim($params);
    }
}

if(!function_exists('fields_filter_trim')){
    /**
     * 方法 fields_filter_trim,兩邊去空
     * @param string/array $data 待處理的字串或陣列
     * @return  string/array $new_data 去空格之後的字串或陣列
     */
    function fields_filter_trim($data){
        //如果有時間欄位則轉為時間戳
        //if(isset($data['start_time'])) $data['start_time'] = strtotime($data['start_time']);
        //if(isset($data['end_time'])) $data['end_time'] = strtotime($data['end_time']);
        //去空
        if(is_array($data)){
            if(!empty($ary)){
                foreach($ary as $k => $v){
                    $data[$k] = trim($v);
                }
            }
        }else{
            $data = trim($data);
        }
        return $data;
    }
}

 

****************************只要思想不滑坡,辦法總比困難多****************************

 

相關文章