PHP获取post传递的json字符串,并将其编码转为utf8

//PHP获取 post数据
function get_raw_post_data() {
    if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
        return $GLOBALS['HTTP_RAW_POST_DATA'];
    } else {
        return file_get_contents('php://input');
    }
}


function get_json_request() {
    $request = get_raw_post_data();
    if ($request == "") {
        return "Parse Error: no data";
    }
    $encoding = mb_detect_encoding($request, 'auto');//获取$request编码
    //convert to unicode
    if ($encoding != 'UTF-8') {
        $request = iconv($encoding, 'UTF-8', $request);//将$request的编码设置为UTF-8
    }
    $request = json_decode($request);//将json字符串转为数组
    if ($request == NULL) { // parse error
        $error = json_error();
        return $error;
    }
    return $request;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页