微信公众号如何生成带参数二维码

2021-11-26   阅读:1745   分类:后端    标签: 微信开发

为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。

目前有2种类型的二维码:

1、临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景 2、永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。

详细demo

<?php
header('content-type:text/html; charset=utf8');
define('TOKEN', 'yooheo'); // TOKEN
define('APP_ID', 'APP_ID'); // 开发者AppID,服务号可获取
define('APP_SECRET', 'APP_SECRET'); // 开发者AppSecret,服务号可获取
set_time_limit(30); //为防止access_token获取失败导致死循环
$wechatObj = new WechatClass();
// echo '二维码地址:'. $wechatObj->QrcodeCreate(111);
echo '<img src="'.$wechatObj->QrcodeCreate(111).'">';

class WechatClass{
  private $token;
  private $appID;
  private $appSecret;
  private $accessToken;
  public function __CONSTRUCT(){
    $this->token   = TOKEN;
    $this->appID   = APP_ID;
    $this->appSecret = APP_SECRET;
    $this->accessToken = './access_token';
    if(!file_exists($this->accessToken)){
      $this->AccessTokenGet();
    }
  }
  /* 创建二维码 @param - $qrcodeID传递的参数,$qrcodeType二维码类型 默认为临时二维码 @return - 返回二维码图片地址*/
  public function QrcodeCreate($qrcodeID, $qrcodeType = 1){
    if($qrcodeType == 0){
      $qrcodeType = 'QR_SCENE';   
    }else{
      $qrcodeType = 'QR_LIMIT_SCENE';
    }     
    $tempJson = '{"expire_seconds": 1800, "action_name": "'.$qrcodeType.'", "action_info": {"scene": {"scene_id": '.$qrcodeID.'}}}';
    $access_token = file_get_contents($this->accessToken);
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    $tempArr = json_decode($this->JsonPost($url, $tempJson), true);
    if(@array_key_exists('ticket', $tempArr)){
      return 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$tempArr['ticket'];
    }else{
      $this->ErrorLogger('qrcode create falied.');
      $this->AccessTokenGet();
      $this->QrcodeCreate();
    }
  }
   
  /* 从微信服务器获取access_token并写入配置文件 */
  private function AccessTokenGet(){
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appID.'&secret='.$this->appSecret;
    $tempArr = json_decode(file_get_contents($url), true);
    if(@array_key_exists('access_token', $tempArr)){       
      $tempWriter = fopen($this->accessToken, 'w');
      fwrite($tempWriter, $tempArr['access_token']);
    }else{
      $this->ErrorLogger('access_token get falied.');
      exit();
    }
  }
  /* 用户分组查询 */
  public function GroupsQuery(){
    $access_token = file_get_contents($this->accessToken);
    $url = 'https://api.weixin.qq.com/cgi-bin/groups/get?access_token='.$access_token;
    $tempArr = json_decode(file_get_contents($url), true);
    if(@array_key_exists('groups', $tempArr)){
      return $tempArr['groups']; //返回数组格式的分组信息
    }else{
      $this->ErrorLogger('groups query falied.');
      $this->AccessTokenGet();
      $this->GroupsQuery();
    }     
  }



  // 工具函数 //
  /* 使用curl来post一个json数据 */
  // CURLOPT_SSL_VERIFYPEER,CURLOPT_SSL_VERIFYHOST - 在做https中要用到
   // CURLOPT_RETURNTRANSFER - 不以文件流返回,带1
  private function JsonPost($url, $jsonData){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    if (curl_errno($curl)) {
      $this->ErrorLogger('curl falied. Error Info: '.curl_error($curl));
    }
    curl_close($curl);
    return $result;
  }
  /* 错误日志记录 */
  private function ErrorLogger($errMsg){
    $logger = fopen('./ErrorLog.txt', 'a+');
    fwrite($logger, date('Y-m-d H:i:s')." Error Info : ".$errMsg."\r\n");
  }
}
?>


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/857.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×