Commit ca93eb6c authored by 杨培's avatar 杨培

支持以 JSON 方式请求数据

parent 84ce032a
...@@ -77,6 +77,23 @@ class Server ...@@ -77,6 +77,23 @@ class Server
$paramsList = array_slice($this->apiMap[$group][$name], 1); $paramsList = array_slice($this->apiMap[$group][$name], 1);
$params = []; $params = [];
//确认请求数据
$contentType = $_SERVER['HTTP_CONTENT_TYPE'] ?? '';
switch ($contentType) {
case 'application/x-www-form-urlencoded':
$requestData = $_POST;
break;
case 'application/json':
$json = file_get_contents('php://input');
$requestData = $json !== '' ? json_decode($json, true): [];
break;
default:
$requestData = [];
}
foreach ($paramsList as $k => $v) { foreach ($paramsList as $k => $v) {
if (is_int($k)) { if (is_int($k)) {
$k = $v; $k = $v;
...@@ -85,8 +102,8 @@ class Server ...@@ -85,8 +102,8 @@ class Server
$hasDefault = true; $hasDefault = true;
} }
if (isset($_POST[$k])) { if (isset($requestData[$k])) {
$params[] = $_POST[$k]; $params[] = $requestData[$k];
} elseif ($hasDefault) { } elseif ($hasDefault) {
$params[] = $v; $params[] = $v;
} else { } else {
...@@ -141,17 +158,31 @@ class Server ...@@ -141,17 +158,31 @@ class Server
$timestamp = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0; $timestamp = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$nonce = isset($_GET['nonce']) ? $_GET['nonce'] : ''; $nonce = isset($_GET['nonce']) ? $_GET['nonce'] : '';
$servTime = time(); //确认用于签名的请求数据
$params = $_POST; $contentType = $_SERVER['HTTP_CONTENT_TYPE'] ?? '';
ksort($params);
$str = $api.http_build_query($params).$clientId.$this->secret.$timestamp.$nonce; switch ($contentType) {
case 'application/x-www-form-urlencoded':
$parr = ksort($_POST);
$pstr = http_build_query($parr);
break;
case 'application/json':
$pstr = file_get_contents('php://input');
break;
default:
$pstr = '';
}
$str = $api.$pstr.$clientId.$this->secret.$timestamp.$nonce;
$servSign = md5($str); $servSign = md5($str);
if ($servSign !== $sign) { if ($servSign !== $sign) {
return false; return false;
} }
if ($servTime - $timestamp > 300) { if (time() - $timestamp > 300) {
return false; return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment