PHP獲取post傳遞的json字串,並將其編碼轉為utf8
阿新 • • 發佈:2019-01-31
//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; }