为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
首先,写一个添加二维码的方法qrcodeAdd(),在方法中,先获取所有的数据存入$arr数组中,把数据存入qrcode表中,在LaneWeChat文件中的Popularize.php中先获取ticket,获取到之后在Popularize.php文件中的getQrcode()方法中处理数据,之后添加到qrcode表中。
public function qrcodeAdd(){
if(IS_GET){
$this->display('qrcode_add');
}else{
$mp=getCurrentMp();
$arr=I('post.');
$arr['mp_id']=$mp['id'];
// print_r($arr);
// exit;
$id=M('qrcode')->add($arr);
//调用创建标签
//$this->createTag($arr['scene_str']);
include APP_PATH . 'LaneWeChat/lanewechat.php';
$ret=Popularize::createTicket($arr['qr_type'],$arr['expire'],$arr['scene_str']);
if(isset($ret['ticket'])){
$qrcodefile=Popularize::getQrcode($ret['ticket']);
$ret['src']=$qrcodefile;
$ret['create_time']=time();
M('qrcode')->where("id=$id")->save($ret);
$this->ajaxReturn(array('status'=>1,'msg'=>'ok','url'=>U('index')));
}else{
$this->ajaxReturn(array('status'=>0,'msg'=>$ret));
}
}
}
在LaneWeChat文件中的Popularize.php的getQrcode()方法:
public static function getQrcode($ticket, $filename=''){
$queryUrl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
$queryAction = 'GET';
$result = Curl::callWebServer($queryUrl, '', $queryAction, 0);
$filename='./Public/qrcode/'.time().'.jpg';
file_put_contents($filename, $result);
return $filename;
}
然后在Public文件下新建一个qrcode文件,把生成的二维码图片存入此文件下。