温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-11-23 10:14:19 来源:亿速云 阅读:171 作者:小新 栏目:开发技术

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

<?php
/**
 * desc:微信js调用类
 * author:besttaowenjing@163.com
 * date:2016-07-04
 */
namespace application\vendors\weixin;
class Jssdk {
    private $appId;
    private $appSecret;
    public function __construct($appId, $appSecret) {
        $this->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;
    }
}
?>

<?php

/**
 * desc:h6签到相关
 * author:besttaowenjing@163.com
 * date:2016-06-22
 */

use application\vendors\weixin\Jssdk;
class SignController extends Controller {
    private $appid = 'xxx';
    private $appsecret = 'xxx';
   

    /**
     * desc:得到活动详情
     */
    public function actionGet_activity() {
        $request = Yii::app()->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如何调用微信接口扫二维码”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

yii
AI