生成二维码

首先写一个qrcodeAdd方法,还要创建一个qrcode数据表,往里边添加数据,检测是否存在ticket,以下是这个方法
public function qrcodeAdd(){
		if(IS_GET){
			$this->display();
		}else{
			$mp = $this->mp;
			$data = I('post.');
			$data['mp_id'] = $mp['id'];
			// dump($data);
			// exit;
			$id = M('qrcode')->add($data);

			include APP_PATH . 'LaneWeChat/lanewechat.php';
			$ret = Popularize::createTicket($data['qr_type'],$data['expire'],$data['scene_str']);
			// dump($ret);
			// exit;

			if(isset($ret['ticket'])){
				$ticket = $ret['ticket'];
				$qrcodefile = Popularize::getQrcode($ticket);
				$ret['src'] = $qrcodefile;
				$ret['create_time'] = time();
				// dump($ret);
				// exit;
				$model = M('qrcode')->where("id = $id")->save($ret);
				$this->ajaxReturn(array('status'=>1,'msg'=>'ok','url'=>U('index')));
			}else{
				$this->ajaxReturn(array('status'=>0,'msg'=>$model));
			}

		}
	}

在这个方法里调用了两个方法在populaize这个文件中,分别传了参数

第一个是createTicket这个方法

 public static function createTicket($type, $expireSeconds, $sceneStr){
        $accessToken=getAccess_token();
        $queryUrl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$accessToken;
        $queryAction = 'POST';
        $template = array();
        if($type == 1){
            $template['expire_seconds'] = $expireSeconds;
            $template['action_name'] = 'QR_STR_SCENE';
        }else if($type == 2){
            $template['action_name'] = 'QR_LIMIT_STR_SCENE';
        }
        $template['action_info']['scene']['scene_str'] = $sceneStr;
        $template = json_encode($template);
        // echo $template;
        // exit;
        return Curl::callWebServer($queryUrl, $template, $queryAction);
    }

第二个是getQrcode方法

public static function getQrcode($ticket){
        $queryUrl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
        // dump($queryUrl);
        // exit;
        $queryAction = 'GET';
        $result = Curl::callWebServer($queryUrl, '', $queryAction, 0);

        $filename = './Public/qrcode/' . time() . '.jpg';

        file_put_contents($filename, $result);
        
        return $filename;
    }
好了,这样二维码就生成了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值