亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - Ask for advice! Regarding the JSSDK error shared by WeChat - invalid signature.
淡淡煙草味
淡淡煙草味 2017-05-24 11:33:32
0
3
858

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

淡淡煙草味
淡淡煙草味

reply all(3)
劉奇

Not familiar with php...

Check whether it is a problem with the url?

世界只因有你
<?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}&timestamp={$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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template