在使用微信js开发过程中页面提示 config:invalid signature
原因:
tp会自动转移html字符,比如’”’转成了 "e; ,所以导致了失败:
错误代码
$access_token = new WeChat(); $url = '' ; $ticket = $access_token->getJsapiTicket(); $ticket = json_decode($ticket)->ticket; $noncestr = $this->createNonceStr(); $timestamp = time(); $string = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url=' . $url; $signature = sha1($string);
正确代码
$access_token = new WeChat(); $url = '' ; $ticket = $access_token->getJsapiTicket(); $ticket = json_decode($ticket)->ticket; $noncestr = $this->createNonceStr(); $timestamp = time(); $string = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '&timestamp=' . $timestamp . '&url=' . $url; $signature1 = sha1(htmlspecialchars_decode($string));
解决思路
使用htmlspecialchars_decode()函数把一些预定义的HTML实体转换为字符,返回转换后的字符串,原字符串不变;