签名机制
<h3>签名机制</h3>
<ul>
<li>
<h5>sha256t签名</h5>
<p>签名机制:sign = hash('sha256', body . apiKey . nonce . timestamp),算法中涉及到字符编码转换时均采用“UTF-8”。
<br>
<font color=orange>注:body:参数,apiKey:签名密钥;nonce:任意随机数(一般为6位),timestamp:时间戳</font></p>
</li>
<li>
<h5>签名示例</h5>
<pre><code>/**
* 1、所涉及演示代码为PHP7以上版本
* 2、框架为ThinkPHP6
* 3、接口请求均POST
* 4、接收字符串参数时不要对反斜杆进行转义
*/
$apiKey = 'E6grwzot2sQ6sGCZ';//密钥
$url = 'https://payapi.likeweb01.com/api/recharge';//请求接口
//请求参数
$data = [
'apiId' => 'SzHxHZPoyFl8sQz',
'orderNo'=>$order['order_no'], //商户订单号
'uid'=>$order['uid'], //商户UID
'orderType'=>1, //充值订单
'amount'=>$order['callback_amount'], //用户充值金额
'address'=>$order['address'] //充值地址
];
//参数转json字符串
$bodyString = json_encode($data);
$timestamp = time();
$nonce = rand(100000, 999999);
//签名规则
$sign = hash('sha256',$bodyString . $apiKey . $nonce . $timestamp);
$postData = array(
'body' => $data,
'timestamp' => $timestamp,
'nonce' => $nonce,
'sign' => $sign
);
$data_string = json_encode($postData);
$data = http_post($url, $data_string);</code></pre>
</li>
<li>
<h5>验签示例</h5>
<pre><code>/**
* 1、所涉及演示代码为PHP7以上版本
* 2、框架为ThinkPHP6
* 3、接口请求均POST
* 4、接收字符串参数时不要对反斜杆进行转义
*/
$body = $request->post('body'); //反斜杆不要转义
$timestamp = $request->post('timestamp');
$nonce = $request->post('nonce');
$sign = $request->post('sign');
//body转为json
$bodyString = json_encode($body);
$mySign = hash('sha256',$bodyString . $api_key . $nonce . $timestamp);
if ($mySign == $sign) { //验签成功
return true;
} else { //验签失败
return false;
}</code></pre>
</li>
<li>
<h5>http_post请求方法</h5>
<pre><code>/**
* 远程访问
* @param string $url
* @param string $data_string
* @return bool|string create on 2022/4/4 19:56
* create on 2022/4/4 19:56
* author 大禹
*/
function http_post(string $url, string $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string)
)
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}</code></pre>
</li>
<li>
<h5>请求方式:POST,参数为JSON</h5>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=2b9b7f168ec757829742b209b7a4852f" alt="" /></p>
</li>
</ul>