微信生成带参数的二维码有两种:
一、临时二维码:
http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式:json POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}} 或者也可以使用以下POST数据创建字符串形式的二维码参数:{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
二、永久二维码:
http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式:json POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}} 或者也可以使用以下POST数据创建字符串形式的二维码参数: {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
生成带参数的二维码逻辑处理:
public function erweima(){
$id = (int)$this->input->get(\'id\');
$data = $this->db->where(\'id\',$id)->get($this->table)->row_array();
$dir = CACHEPATH."ercode/zhuanjia/";
sys_mkdirs($dir);
if(!file_exists($dir."/qrcode_".$id.".jpg")){
$jsonarr= $this->dweixin->get_wxma(\'ZHUANJIA_\'.$id,1);
$path_1 = \'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=\'.$jsonarr[\'ticket\'].\'\';
// 图片二
$image_1 = imagecreatefromstring(file_get_contents($path_1));
imagejpeg($image_1, $dir."qrcode_".$id.".jpg");
}
$img = SITE_URL.\'cache/ercode/zhuanjia/qrcode_\'.$id.\'.jpg?id=\'.SYS_TIME.\'\';
$this->template->assign(array(
\'imagesrc\' => $img,
));
$this->template->display(APP_C.\'_erweima.html\');
}
public function get_wxma($kid,$type=0){
$acctoken = $this->red_acctoke();//获取token
$poststr=\'{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id":\'.$kid.\'}}}\';
if($type){
$poststr=\'{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str":"\'.$kid.\'"}}}\';
}
$json = $this->post_url("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=". $acctoken ."",$poststr);
$json_data = json_decode($json,true);
if($json_data["errcode"]){
$this->log_txt(FCPATH."/dweixin-err.txt",\'生成二维码失败token---\'.$acctoken.\' --------type:\'.$type.\'--kid:\'.$kid.\' \'.$poststr.\'- \'.$json);
return false;
}
return $json_data;
}
扫描二维码的逻辑处理:
2 扫描带参数二维码事件(微信公众号消息推送中的事件)
public function responseMsg(){
$postStr = file_get_contents("php://input");
if(!empty($postStr)){
$postObj = simplexml_load_string($postStr, \'SimpleXMLElement\', LIBXML_NOCDATA);
$this->Uopenid = $postObj->FromUserName;
$this->Username = $postObj->ToUserName;
$this->keyword = trim($postObj->Content);
$msgType = $postObj->MsgType;
// $this->dweixin->log_txt("weixin_saoma.txt",$postObj->EventKey.\'test--1111-无法错误\'.$msgType);
switch($msgType){
case "event":
$event = $postObj->Event;
switch($event){
case "subscribe": //关注
$resultStr=$this->E_guanzhu($postObj->EventKey);
break;
case "unsubscribe": //取消关注
$resultStr=$this->E_qxguazhu();
break;
case "CLICK": //按钮事件
//$resultStr = $this->E_keymsg($postObj->EventKey);
$this->Ehuoyue();
break;
case "SCAN": //扫码
$resultStr = $this->E_saoma($postObj->EventKey);
break;
case "LOCATION": //上报地理位置
$this->Latitude = $postObj->Latitude;
$this->Longitude = $postObj->Longitude;
$this->Precision = $postObj->Precision;
$this->E_useraddr();
break;
case "MASSSENDJOBFINISH": //接受群发事件推送
$msg[\'MsgID\'] = $postObj->MsgID;
$msg[\'Status\'] = $postObj->Status;
$msg[\'TotalCount\'] = $postObj->TotalCount;
$msg[\'FilterCount\'] = $postObj->FilterCount;
$msg[\'SentCount\'] = $postObj->SentCount;
$msg[\'ErrorCount\'] = $postObj->ErrorCount;
$this->E_sendmsg($msg);
break;
}
break;
case "text":
$this->Ehuoyue();
$resultStr=$this->E_keymsg($this->keyword);
break;
case "image":
$resultStr=$this->M_image();
break;
case "voice":
$resultStr=$this->M_voice();
break;
case "video":
$resultStr=$this->M_video();
break;
case "location":
$resultStr=$this->M_location();
break;
case "link":
$resultStr=$this->M_link();
break;
}
if(!$resultStr){
$this->Ehuoyue();
echo "success";
//$this->dweixin->log_txt(FCPATH."/Weixin_EventKey.txt",\'test---无法错误\');
exit;
}
echo $resultStr;
exit;
}
}
//关注
private function E_guanzhu($keys=\'\'){
$guser = $this->adduser($this->Uopenid);
$ulogo = \'\';
if(isset($guser[\'headimgurl\'])){
$avatar = explode(\'/\',$guser["headimgurl"]);
unset($avatar[count($avatar)-1]);
$avatar[] = 132;
$ulogo = implode(\'/\',$avatar);
}
$openid = $guser[\'openid\'];
$nickname = preg_replace(\'/[\x{10000}-\x{10FFFF}]/u\', \'\', $guser[\'nickname\']);
$avatar = $ulogo;
$subscribe = $guser[\'subscribe\'];
$user = $this->add_wxuser($this->Uopenid , array(\'nickname\'=>$nickname, \'avatar\'=>$avatar,\'subscribe\'=>$subscribe));
if (is_array($user)){
$msgxml = \'尊敬的会员\'.$user[\'nickname\'].\',感谢您的关注!\';
$msg = $this->dweixin->huifu_xml($this->Username,$this->Uopenid,\'text\',$msgxml);
$this->db->insert(\'member_loginlog\', array(\'uid\'=>$user[\'uid\'],\'url\'=>\'subscribe\',\'addtime\'=>time()));
if(stristr($keys,\'qrscene_\')){
$kid = str_replace(\'qrscene_\',\'\',$keys);
$msg = $this->E_saoma($kid,0);
}
$this->Ehuoyue();
}
return $msg;
}
//扫描带参数二维码
private function E_saoma($kid,$type=1){
// $this->dweixin->log_txt("weixin_saoma.txt",\'test--1111-无法错误\'.$kid);
$this->Ehuoyue();
if(!$kid) return;
$guser = $this->adduser($this->Uopenid);
$ulogo = \'\';
if(isset($guser[\'headimgurl\'])){
$avatar = explode(\'/\',$guser["headimgurl"]);
unset($avatar[count($avatar)-1]);
$avatar[] = 132;
$ulogo = implode(\'/\',$avatar);
}
$openid = $guser[\'openid\'];
$nickname = preg_replace(\'/[\x{10000}-\x{10FFFF}]/u\', \'\', $guser[\'nickname\']);
$avatar = $ulogo;
$subscribe = $guser[\'subscribe\'];
$user = $this->add_wxuser($this->Uopenid , array(\'nickname\'=>$nickname, \'avatar\'=>$avatar,\'subscribe\'=>$subscribe));
$data = array();
$_mastr = explode(\'_\', $kid);
$this->dweixin->log_txt("weixin-err.txt",json_encode($user).\'--\'.$kid);
//addyg-店类型-店ID
switch($_mastr[0]){
case "ZHUANJIA":
$msgtype = \'news\';
$data = $this->db->where(\'id\',$_mastr[1])->get(\'member_zhuanjia\')->row_array();
$msg[\'title\'] = $data[\'name\'];
$msg[\'description\'] = \'点击查看专家【\'.$data[\'name\'].\'】\';
$msg[\'thumb\'] = sys_image($data[\'avatar\']);
$msg[\'url\'] = SITE_URL.\'html/sxjy/html/ask/expert_index.html?id=\'.$data[\'uid\'];
$msgxml[] = $msg;
break;
case "SCHOOL":
$msgtype = \'news\';
$data = $this->db->where(\'id\',$_mastr[1])->get(\'zixun_school\')->row_array();
$msg[\'title\'] = $data[\'title\'];
$msg[\'description\'] = \'点击查看院校【\'.$data[\'title\'].\'】\';
$msg[\'thumb\'] = sys_image($data[\'logo\']);
$msg[\'url\'] = SITE_URL.\'html/sxjy/html/ask/school_index.html?id=\'.$_mastr[1];
$msgxml[] = $msg;
break;
default:
break;
}
$huifumsg = $this->dweixin->huifu_xml($this->Username,$this->Uopenid, $msgtype ,$msgxml);
return $huifumsg;
}