微信分享签名无效的问题
微信签名无效问题处理
微信登录后前端页面上的地址其实已经变了,首页地址后面加上了一串code参数,刚开始以为分享地址就是首页地址,直接在服务端代码里写死了,生成签名后返给前端页面,前端页面一直提醒签名无效,在微信js接口签名校验工具中(https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign),生成的签名是正确的,各种找问题,后来突然发现微信登录首页地址后面有参数,所以让前端传给服务端
一个地址,然后去生成分享签名参数,分享成功。
public function getSignPackage($urls) {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 一定要动态获取,不能 hardcode.
//$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
//$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//$url = $protocol.$url;
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$urls";
$signature = sha1($string);
$signPackage = array(
"appId" => config('wx_conf')['appid'],
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $urls,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}