第一步:根据openid和secret获取access_token;示例代码如下:
$url = 'https://api./cgi-bin/token?grant_type=client_credential&appid='. $this->appid .'&secret='.$this->secret;$arr = file_get_contents($url);$arr = json_decode($arr,true);$access_token = $arr['access_token'];
第二步:根据access_token获取获取ticket;示例代码如下:
//请求地址$qrcode_url = 'https://api./cgi-bin/qrcode/create?access_token=' . $access_token;$scene_id = 1231;// 传参自定义参数 微信端可获取 文档规定 是一个32位无符号整数//设置请求参数 具体说明在微信文档中可查 特别说明 scene_id一定要与事件推送回调中接收的名字一样有的会定义成scene_str$postData = '{"expire_seconds": 86400, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';// 发起post请求 获取ticket$res = $this->curlPost($qrcode_url,$postData);$res = json_decode($res,true);
第三步:拼接url用于生成二维码,需要说明一下ticket需要urlencode一下;示例代码如下:
$url = 'https://mp./cgi-bin/showqrcode?ticket=' . urlencode($res['ticket']);
至此完成了生成微信公众号二维码的接口了。欢迎大家在评论区留言,我一定会及时回应。
点击微信公众号文档查看官方介绍