签名规则
<ul>
<li>为了防止API调用过程中被恶意篡改,调用任何一个API都需要携带签名。TOP服务端会根据请求参数,对签名进行验证,任何签名不合法的请求都将被拒绝。签名过程如下:</li>
</ul>
<p><div style="padding:0px 10px;"><strong>1.</strong> 对所有API请求参数(包括公共参数和业务参数,但<strong>sign</strong>参数不参与签名),根据参数名进行<strong>ASCII</strong>码表的升序排序。</div><div style="padding:0px 26px;"><strong>例如请求参数:</strong></div><div style="padding:0px 40px;">{&quot;method&quot;:&quot;PRE_ORDER&quot;,&quot;appid&quot;:123456,&quot;timestamp&quot;:&quot;1662701976819&quot;,&quot;v&quot;:&quot;1.0&quot;,&quot;channelTag&quot;:&quot;智能&quot;,&quot;sender&quot;:&quot;收件人&quot;,&quot;senderMobile&quot;:&quot;13811122233&quot;,&quot;senderProvince&quot;:&quot;辽宁省&quot;}</div><div style="padding:0px 26px;"><strong>排序后的顺序:</strong> </div><div style="padding:0px 40px;">{&quot;appid&quot;:123456,&quot;channelTag&quot;:&quot;智能&quot;,&quot;method&quot;:&quot;PRE_ORDER&quot;,&quot;sender&quot;:&quot;收件人&quot;,&quot;senderMobile&quot;:&quot;13811122233&quot;,&quot;senderProvince&quot;:&quot;辽宁省&quot;,&quot;timestamp&quot;:&quot;1662701976819&quot;,&quot;v&quot;:&quot;1.0&quot;}</div>
<div style="padding:20px 10px 0px 10px;"><strong>2.</strong> 把排序后的所有参数值拼装在一起,在最后面在加上AppSecret值(例如AppSecret=abcdefg)<div><div style="padding:0px 26px;"><strong>拼接后字符串:</strong>123456智能PRE_ORDER收件人13811122233辽宁省16627019768191.0abcdefg</div><div style="padding:10px 0px 0px 0px;color:#FF0000;">注意:AppSecret仅用于签名生成过程,并不作为请求的一部分进行传输,请务必妥善保管。任何泄露AppSecret的行为都可能导致API被盗用造成损失。</div></p>
<p><div style="padding:20px 0px 0px 0px;"><strong>3.</strong> 对拼装好的字符串进行摘要,将MD5摘要结果转为大写后即是 <strong>sign签名</strong><div><div style="padding:0px 26px;"><strong>例:</strong>sign=strtoupper(MD5(&quot;123456智能PRE_ORDER收件人13811122233辽宁省16627019768191.0abcdefg&quot;))</div></p>
<p><h4 style="margin-bottom: 30px;"> </h4></p>
<p><h4 style="margin-bottom: -10px;"> 签名示例 </h4></p>
<ul>
<li>
<p>PHP</p>
<pre><code>// 假设的API请求参数
$params = [
'method' =&gt; 'PRE_ORDER',
'appid' =&gt; '123456',
'timestamp' =&gt; '1662701976819',
'v' =&gt; '1.0',
'channelTag' =&gt; '智能',
'sender' =&gt; '收件人',
'senderMobile' =&gt; '13811122233',
'senderProvince' =&gt; '辽宁省'
];
$appSecret = 'abcdefg'; // 实际的AppSecret,请替换为您的AppSecret
/**
* 生成签名
*
* @param array $params API请求参数
* @param string $appSecret AppSecret密钥
* @return string 生成的签名
*/
function generateMd5Sign($params, $appSecret) {
unset($params['sign']); // 移除sign键值
ksort($params); // ksort() 函数根据键名对数组进行排序
$stringToBeSigned = '';
foreach ($params as $value) {
$stringToBeSigned .= $value; // 拼接所有参数值
}
$stringToBeSigned .= $appSecret; // 在最后面加上AppSecret
$sign = strtoupper(md5($stringToBeSigned)); // 使用md5()函数生成摘要,并使用strtoupper()转为大写
return $sign; // 返回生成的签名
}
$sign = generateMd5Sign($params, $appSecret); // 生成签名
echo &quot;Sign: &quot; . $sign; // 输出签名 Sign: 21102C6CDF0CB963DFEA822F947EE092</code></pre>
</li>
</ul>