The signature has been generated, and it is the same as the signature generated by the WeChat signature testing tool. When scanning the QR code to access the page, invalid signature still pops up. How to solve it? ! Please give me some advice!
image description
Generated by WeChat
The one generated by yourself and WeChat
<?php
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = getJsapiTicket();
if($jsapiTicket&&$jsapiTicket<>''){
// 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode.
$protocol = (!empty($_SERVER ['HTTPS']) && $_SERVER ['HTTPS'] !== 'off' || $_SERVER ['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序
$string = "jsapi_ticket={$jsapiTicket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}else{
return false;
}
}
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i ++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
上面是開源的庫(kù),調(diào)用方法就算getSignPackage(),執(zhí)行之后得到nonceStr、signature、timestamp
$jssdk =new \JSSDK(C('APPID'), C('APPSECRET'));
$data=$jssdk->getSignPackage();
$this->assign('nonceStr', $data['nonceStr']);
$this->assign('signature', $data['signature']);
$this->assign('appid', C('APPID'));
$this->assign('timestamp', $data['timestamp']);
我記得公眾平臺(tái)的后臺(tái)有個(gè)jssdk的安全目錄要設(shè)置的。
Confirm that the appid in config is consistent with the appid used to obtain jsapi_ticket
The best way is to download the WeChat development tool and debug all parameters on the computer side