使用微信扫一扫时获取sign签名

private $appId = '你的APPID';
private $appSecret = '你的APPsecret';
/**
     * 获取签名信息
     * @return array
     */
    public function getSignPackage()
    {
        $jsapiTicket = $this->getJsApiTicket();
        $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();
        // 这里参数的顺序要按照 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
        );
        return $signPackage;
    }

    /**
     * 获取access_token
     * @return mixed
     * 因为每天最多只能向微信请求200次access_token,所以要存到缓存里或者本地文件中
     */
    private function getAccessToken()
    {
        if(Cache::get('access_token'))
        {
            $access_token = Cache::get('access_token');
            return $access_token;
        }
        else
        {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
            $res = json_decode(file_get_contents($url));
            $access_token = $res->access_token;
            Cache::put('access_token',$access_token,7000);
            return $access_token;
        }
    }

    /**
     * 获取ticket
     * @return mixed
     */
    private function getJsApiTicket()
    {
        if(Cache::get("jsapi_ticket"))
        {
            $jsapi_ticket = Cache::get('jsapi_ticket');
            return $jsapi_ticket;
        }
        else
        {
            if(Cache::get('access_token'))
            {
                $accessToken = Cache::get('access_token');
                $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
                $res = json_decode(file_get_contents($url));
                $ticket = $res->ticket;
                Cache::put('jsapi_ticket',$ticket,7000);
                return $ticket;
            }
            else
            {
                $accessToken = $this->getAccessToken();
                $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
                $res = json_decode(file_get_contents($url));
                $ticket = $res->ticket;
                Cache::put('jsapi_ticket',$ticket,7000);
                return $ticket;
            }
        }
    }

    /**
     * 随机数生成算法
     * @param int $length
     * @return string
     */
    private function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
posted @ 2019-12-16 16:58  你的男孩  阅读(1028)  评论(0编辑  收藏  举报