千家信息网

yii如何调用微信接口扫二维码

发表于:2024-11-15 作者:千家信息网编辑
千家信息网最后更新 2024年11月15日,这篇文章主要介绍yii如何调用微信接口扫二维码,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
千家信息网最后更新 2024年11月15日yii如何调用微信接口扫二维码

这篇文章主要介绍yii如何调用微信接口扫二维码,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

appId = $appId;        $this->appSecret = $appSecret;    }        public function getSignPackage(){        $jsapiTicket = $this->getJsApiTicket();        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";        $timestamp = time();        $nonceStr = $this->createNonceStr();        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";        $signature = sha1($string);        $signPackage = array(            "appId" => $this->appId,            "nonceStr" => $nonceStr,            "timestamp" => $timestamp,            "url" => $url,            "signature" => $signature,            "rawString" => $string        );        return $signPackage;    }    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 getJsApiTicket() {        // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例        $data = json_decode(file_get_contents(dirname(__FILE__).'/jsapi_ticket.json'));        //$data = (object)array();        if ($data->expire_time < time()) {            $accessToken = $this->getAccessToken();            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";            $res = json_decode($this->httpGet($url));            $ticket = $res->ticket;            if ($ticket) {                $data->expire_time = time() + 7000;                $data->jsapi_ticket = $ticket;                $fp = fopen(dirname(__FILE__)."/jsapi_ticket.json", "w");                fwrite($fp, json_encode($data));                fclose($fp);            }        } else {            $ticket = $data->jsapi_ticket;        }        return $ticket;    }    private function getAccessToken() {        // access_token 应该全局存储与更新,以下代码以写入到文件中做示例        $data = json_decode(file_get_contents(dirname(__FILE__)."/access_token.json"));          //$data = (object)array();        if ($data->expire_time < time()) {            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";            $res = json_decode($this->httpGet($url));            $access_token = $res->access_token;            if ($access_token) {                $data->expire_time = time() + 7000;                $data->access_token = $access_token;                $fp = fopen(dirname(__FILE__)."/access_token.json", "w");                fwrite($fp, json_encode($data));                fclose($fp);            }        } else {            $access_token = $data->access_token;        }        return $access_token;    }    function httpGet($url) {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);        // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        $temp = curl_exec($ch);        curl_close($ch);        return $temp;    }}?>request;        $userId = intval($request->getParam('userid'));        if ($userId < 1) {            echo '验证失败';            exit;        }        $activityId = intval($request->getParam('activityid'));        if ($activityId < 1) {            echo '验证失败';            exit;        }        $criteria = new CDbCriteria;        $criteria->select = 'id, title, address, starttime, num';        $activity = Activity::model()->findbypk($activityId, $criteria);        if (empty($activity)) {            echo '您所查询的数据不存在';exit;        }        $jssdk = new Jssdk($this->appid, $this->appsecret);        $signPackage = $jssdk->getSignPackage();                $data = array();        $token = md5(microtime(true));        Yii::app()->session['signtoken'] = $token;        $data['userid'] = $userId;        $data['activity'] = $activity;        $data['token'] = $token;        $data['signPackage'] = $signPackage;        $this->renderPartial('sign', $data);    }        /**     * desc:json输出     */    private function _showMsg($data) {        echo json_encode($data);        exit;    }}

以上是"yii如何调用微信接口扫二维码"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0