Commit ca93eb6c authored by 杨培's avatar 杨培

支持以 JSON 方式请求数据

parent 84ce032a
......@@ -77,6 +77,23 @@ class Server
$paramsList = array_slice($this->apiMap[$group][$name], 1);
$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) {
if (is_int($k)) {
$k = $v;
......@@ -85,8 +102,8 @@ class Server
$hasDefault = true;
}
if (isset($_POST[$k])) {
$params[] = $_POST[$k];
if (isset($requestData[$k])) {
$params[] = $requestData[$k];
} elseif ($hasDefault) {
$params[] = $v;
} else {
......@@ -141,21 +158,35 @@ class Server
$timestamp = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$nonce = isset($_GET['nonce']) ? $_GET['nonce'] : '';
$servTime = time();
$params = $_POST;
ksort($params);
$str = $api.http_build_query($params).$clientId.$this->secret.$timestamp.$nonce;
//确认用于签名的请求数据
$contentType = $_SERVER['HTTP_CONTENT_TYPE'] ?? '';
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);
if ($servSign !== $sign) {
return false;
}
if ($servTime - $timestamp > 300) {
if (time() - $timestamp > 300) {
return false;
}
return $api;
}
}
\ No newline at end of file
}
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