公众号生成带推荐码(场景id)的二维码

<?php
namespace BusinessController;
use BusinessControllerBaseController;
use ThinkHook;

/**
 * 关注公众号 
 * @author 戈丫汝<534208139@qq.com>
 */
class WxqrcodeController{
    protected $appid = '';
    protected $secret = '';
    protected $url = "";
    protected $access_tokens = "";

    public function __construct(){
        $mpid = $_GET['mpid'];
        $iinfo = M('test')->where(['id'=>$mpid])->find();
        $this->appid = $iinfo['appid'];
        $this->secret = $iinfo['appsecret'];
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret . "";
        $result = $this->curl_post($url);
        $access_tokens = json_decode($result, true);
        $this->access_tokens = $access_tokens['access_token'];
    }
    //打印二维码显示
    public function Follow(){
        $scene_str = $_GET['id'];
        $rs = $this->getTemporaryQrcode($this->access_tokens, $scene_str);
        $ticket = $rs['ticket'];
        $qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket . "";
        echo $qrcode;
    }
    //生成二维码
    public function getTemporaryQrcode($access_tokens,$orderId=''){
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" .$access_tokens . "";
        //生成二维码需要的参数
        $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": ' . $orderId . '}}}';
        $momo = json_decode($qrcode, true);
        $result = $this->curl_post($url, $momo);
        $rs = json_decode($result, true);
        return $rs;
    }
    function curl_post($url, array $params = array()){
        $data_string = json_encode($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/json'
            )
        );
        $data = curl_exec($ch);
        curl_close($ch);
        return ($data);
    }
}

 生成二维码为扫描关注公众号,我们业务需求实现结果是,在微信关注或者scan事件监听场景id(推荐码),绑定成功后公众号推送消息.

注意:

  公众号场景二维码scenceid限制最大数字为 100000 ! 超过的生成不了

原文地址:https://www.cnblogs.com/gyrgyr/p/13517763.html