簽名錯誤首先去官網看一下簽名生成是否正確,以及錯誤原因
鏈接:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
獲取 access_token
function token(){
$appid ="xxxxx";
$secret = "xxxxx";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$data = file_get_contents($url);
$arr = json_decode($data,true);
$access_token = $arr['access_token'];
return $access_token;
}
$token = token();
獲取票據 jsapi_ticket 需要傳入access_token
function ticket( $token ){
$js_t = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token."&type=jsapi";
$datas = file_get_contents($js_t);
$arrs = json_decode( $datas ,true);
$ticket = $arrs['ticket'];
return $ticket;
}
$ticket = ticket( $token );
簽名算法
$params = [
'noncestr' => uniqid(),
'jsapi_ticket' => $ticket ,
'timestamp' => time(),
'url' => 'xxxxxxxxxxxx'
];
ksort($params);
js接口安全域名設置
進入公衆號
按照微信公衆平臺的開發文檔,簽名錯誤排查方法如下:
invalid signature簽名錯誤建議按如下順序檢查:
(1)確認簽名算法正確,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 頁面工具進行校驗。
(2)確認config中nonceStr(js中駝峯標準大寫S), timestamp與用以簽名中的對應noncestr, timestamp一致。
(3)確認url是頁面完整的url(請在當前頁面alert(location.href.split('#')[0])確認),包括'http(s)://'部分,以及'?'後面的GET參數部分,但不包括'#'hash後面的部分。
(4)確認 config 中的 appid 與用來獲取 jsapi_ticket 的 appid 一致。
(5)確保一定緩存access_token和jsapi_ticket。
(6)確保你獲取用來簽名的url是動態獲取的,動態頁面可參見實例代碼中php的實現方式。如果是html的靜態頁面在前端通過ajax將url傳到後臺簽名,前端需要用js獲取當前頁面除去'#'hash部分的鏈接(可用location.href.split('#')[0]獲取,而且需要encodeURIComponent),因爲頁面一旦分享,微信客戶端會在你的鏈接末尾加入其它參數,如果不是動態獲取當前鏈接,將導致分享後的頁面簽名失敗。