公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
目前有2种类型的二维码:
1、临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
2、永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
用户扫描带场景值二维码时,可能推送以下两种事件:
如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码。
每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id:整数)或者(scene_str:字符串)。
public function qrcodeAdd()
{
if (IS_GET) {
$this->display('qrcode_add');
}else{
$mp = $this->mp;
$arr = I('post.');
$arr['mpid'] = $mp['id'];
$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));
}
}
}
public function createTag($tagname=''){$mp = $this->mp; $where['mpid'] = $mp['id'];$where['tag'] = $tagname;$data = M("tags")->where($where)->find();if(empty($data)){$api = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=".getAccess_token();$arr = array();$arr['tag']['name'] = $tagname;$json = json_encode($arr,JSON_UNESCAPED_UNICODE);include APP_PATH .'LaneWeChat/lanewechat.php';$ret=Curl::callWebServer($api,$json,'POST');if($ret['tag']){$row['mpid'] = $mp['id'];$row['tag_id'] = $ret['tag']['id'];$row['tag'] = $ret['tag']['name'];M('tags')->add($row);}}}通过ticket换取二维码图片
接口:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET,TICKET记得进行UrlEncode
该操作无需登录或授权即可调用,也就是说可以直接在浏览器中打开该链接获取图片。
例如打开以上永久二维码的id图片:
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQH88DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyYk44LXNGeEZlcGkxMDAwMHcwM2gAAgRiubxZAwQAAAAA

给粉丝打标签
public function createTag($tagname='')
{
$mp = $this->mp;
$where['mpid'] = $mp['id'];
$where['tag'] = $tagname;
$data = M("tags")->where($where)->find();
if(empty($data)){
$api = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=".getAccess_token();
$arr = array();
$arr['tag']['name'] = $tagname;
$json = json_encode($arr,JSON_UNESCAPED_UNICODE);
include APP_PATH .'LaneWeChat/lanewechat.php';
$ret=Curl::callWebServer($api,$json,'POST');
if($ret['tag']){
$row['mpid'] = $mp['id'];
$row['tag_id'] = $ret['tag']['id'];
$row['tag'] = $ret['tag']['name'];
M('tags')->add($row);
}
}
}
public function openidTag()
{
$openid = "oA8uR0pY3p9WLgrUHWdK1TNWte-I";
$api = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".getAccess_token();
$data['openid_list'] = array($openid);
$data['tagid'] = 109;
$json = json_encode($data,JSON_UNESCAPED_UNICODE);
include APP_PATH . 'LaneWeChat/lanewechat.php';
$ret = Curl::callWebServer($api,$json,'POST');
print_r($ret);
}
同步粉丝并在粉丝页获取标签
public function friends($tagid='')
{
$mp = $this->mp;
$where['mpid'] = $mp['id'];
if (!empty($tagid)) {
$where['tagid_list'] = array('like',"%{$tagid}%");
}
$data=M('mp_friends')->where($where)->select();
$tag = M('tags')->where($where)->select();
$this->assign('tag',$tag);
$this->assign('data',$data);
$this->display();
}
public function Sycfriends()
{
$mp = $this->mp;
$access_token = getAccess_token();
include APP_PATH . 'LaneWeChat/lanewechat.php';
$api = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";
$ret = Curl::callWebServer($api,$data,'post',true,false);
for ($i=0; $i <$ret['count']; $i++) {
$openid = $ret['data']['openid'][$i];
$openids[$i]['openid'] = $openid;
$openids[$i]['lang'] = 'zh_CN';
}
$data = userManage::getManyUserInfo($openids);
if (isset($data['user_info_list'])) {
$data = $data['user_info_list'];
foreach ($data as &$value) {
$value['mpid'] = $mp['id'];
$value['tagid_list'] = implode(',',$value['tagid_list']);
}
}
$openid=M('mp_friends')->where("openid = '$openid'")->find();
if (!isset($openid)) {
M('mp_friends')->addAll($data);
}
$this->redirect('friends');
}