Signature.php
2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace App\Common\TencentMQ;
/**
* Sign
* 签名类
*/
class Signature
{
/**
* sign
* 生成签名
* @param string $srcStr 拼接签名源文字符串
* @param string $secretKey secretKey
* @param string $method 请求方法
* @return
*/
public static function sign($srcStr, $secretKey, $method = 'HmacSHA1')
{
switch ($method) {
case 'HmacSHA1':
$retStr = base64_encode(hash_hmac('sha1', $srcStr, $secretKey, true));
break;
case 'HmacSHA256':
$retStr = base64_encode(hash_hmac('sha256', $srcStr, $secretKey, true));
break;
default:
throw new Exception($method . ' is not a supported encrypt method');
return false;
break;
}
return $retStr;
}
/**
* makeSignPlainText
* 生成拼接签名源文字符串
* @param array $requestParams 请求参数
* @param string $requestMethod 请求方法
* @param string $requestHost 接口域名
* @param string $requestPath url路径
* @return
*/
public static function makeSignPlainText($requestParams,
$requestMethod = 'POST', $requestHost = YUNAPI_URL,
$requestPath = '/v2/index.php')
{
$url = $requestHost . $requestPath;
// 取出所有的参数
$paramStr = self::_buildParamStr($requestParams, $requestMethod);
$plainText = $requestMethod . $url . $paramStr;
return $plainText;
}
/**
* _buildParamStr
* 拼接参数
* @param array $requestParams 请求参数
* @param string $requestMethod 请求方法
* @return
*/
protected static function _buildParamStr($requestParams, $requestMethod = 'POST')
{
$paramStr = '';
ksort($requestParams);
$i = 0;
foreach ($requestParams as $key => $value)
{
if ($key == 'Signature')
{
continue;
}
// 排除上传文件的参数
if ($requestMethod == 'POST' && substr($value, 0, 1) == '@') {
continue;
}
// 把 参数中的 _ 替换成 .
if (strpos($key, '_'))
{
$key = str_replace('_', '.', $key);
}
if ($i == 0)
{
$paramStr .= '?';
}
else
{
$paramStr .= '&';
}
$paramStr .= $key . '=' . $value;
++$i;
}
return $paramStr;
}
}