/**
* 获取请求类型
* @time 2019-06-03 10:55
* @return |null
*/
public static function getContentType(){
if (isset($_SERVER['CONTENT_TYPE'])) {
return $_SERVER['CONTENT_TYPE'];
}
if (isset($_SERVER['HTTP_CONTENT_TYPE'])) {
return $_SERVER['HTTP_CONTENT_TYPE'];
}
return null;
}
/**
* 获取参数
* @time 2019-06-03 11:37
* @return array|mixed
*/
public static function getBodyParams(){
$rawContentType = self::getContentType();
if (($pos = strpos($rawContentType, ';')) !== false) {
$contentType = strtolower(substr($rawContentType, 0, $pos));
} else {
$contentType = strtolower($rawContentType);
}
$_bodyParams = [];
$method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
if('application/json' === $contentType){
$_bodyParams = json_decode(file_get_contents('php://input'),true);
}else if($method === 'POST'){
$_bodyParams = $_POST ? $_POST : $GLOBALS['HTTP_RAW_POST_DATA'];
}else if($method === 'GET'){
$_bodyParams = $_GET;
}else if($method === 'REQUEST'){
$_bodyParams = $_REQUEST;
}else{
mb_parse_str(file_get_contents('php://input'), $_bodyParams);
}
return $_bodyParams;
}
网友评论