临时二维码
实例
临时二维码
<?php
/**
* 生成二维码接口
* 文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
* 类型:临时二维码 永久二维码
*/
//1.获取ticket
$access_token = "11_kjKUYYOebaF1O1GiuX0o-u3AfyGupEJciVGE1__z6G9RBwQlRV1IEdtiCLOj0dwDK0daTGJjjiaMgSxpXJ51wwOiqsocGQQYJp4W1USuq8MFUd0igYa5KbjOpcvzobUiaPind7H38yP0rDJxNITfACAVQB";
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$postArr = [
'expire_seconds' => '604800',//单位秒
'action_name' => 'QR_SCENE',
'action_info' => [
'scene' => ['scene_id'=>10000]//场景值ID,自定义
]
];
$postJson = json_encode($postArr);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postJson);
$output = curl_exec($ch);
curl_close($ch);
/*返回结果
{
"ticket":"gQH-7zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyLTBtek5aTElmamsxajdlbzFyMW0AAgRH1E5bAwSAOgkA",
"expire_seconds":604800,
"url":"http://weixin.qq.com/q/02RlzMN9LIfjk1hDdoNr1F"
}
*/
$ticket = $output['ticket'];
//2.使用ticket获取二维码图片
$qrUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);//ticket记得进行UrlEncode
//展示:浏览器直接get请求$qrUrl地址
永久二维码
<?php
/**
* 生成二维码接口
* 文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
* 类型:临时二维码 永久二维码
*/
//1.获取ticket
$access_token = "11_kjKUYYOebaF1O1GiuX0o-u3AfyGupEJciVGE1__z6G9RBwQlRV1IEdtiCLOj0dwDK0daTGJjjiaMgSxpXJ51wwOiqsocGQQYJp4W1USuq8MFUd0igYa5KbjOpcvzobUiaPind7H38yP0rDJxNITfACAVQB";
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$postArr = [
'action_name' => 'QR_LIMIT_SCENE',
'action_info' => [
'scene' => ['scene_id'=>20000]//场景值ID,自定义
]
];
$postJson = json_encode($postArr);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postJson);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
/*返回结果
{
"ticket":"gQH-7zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyLTBtek5aTElmamsxajdlbzFyMW0AAgRH1E5bAwSAOgkA",
"expire_seconds":604800,
"url":"http://weixin.qq.com/q/02RlzMN9LIfjk1hDdoNr1F"
}
*/
$ticket = $output['ticket'];
//2.使用ticket获取二维码图片
$qrUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);//ticket记得进行UrlEncode
//展示:浏览器直接get请求$qrUrl地址
扫描带参数二维码事件
<?php
/**
* 扫描带参数二维码事件
*/
$postArr = file_get_contents('php://input');
$postObj = simplexml_load_string($postArr,'SimpleXMLElement', LIBXML_NOCDATA);
if(strtolower($postObj->MsgType) == 'event') {
//未关注扫描二维码
if(strtolower($postObj->Event) == 'subscribe'){
if($postObj->EventKey == "qrscene_10000"){//生成二维码图片的场景ID,生成时设置,未关注前面有个前缀,qrscene
$Content = "临时二维码关注你";
}else{
$Content = "永久二维码关注你";
}
}
//重扫:关注后扫描二维码
if(strtolower($postObj->Event) == 'scan'){
if($postObj->EventKey == 10000){//生成二维码图片的场景ID,生成时设置
$Content = "临时二维码欢迎你";
}else{
$Content = "永久二维码欢迎你";
}
}
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$MsgType = 'text';
$template = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
$info = sprintf($template,$toUser,$fromUser,$time,$MsgType,$Content);
echo $info;
}