• 使用微信扫一扫时获取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;
        }
  • 相关阅读:
    (周日赛) Average is not Fast Enough!
    过山车
    (寒假CF3)B
    (寒假CF3)坑坑坑
    (周六赛1)Sum it up
    畅通工程
    vue 动态添加form表单item 校验问题
    html2canvas转pdf分页隔断问题处理
    vue中html转pdf并下载功能
    一个简单的滑动溢出效果
  • 原文地址:https://www.cnblogs.com/wxy0126/p/12049988.html
Copyright © 2020-2023  润新知