PHP微信公众平台开发高级篇--生成二维码接口

临时二维码

实例

临时二维码

<?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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JasonHome

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值