微信获取带参数二维码

获取带参数二维码,正常流程一般分为三步:

1.获取access_token

2.根据token和scene_id获取ticket

3.根据ticket获取二维码链接

有什么不懂可以看看官方文档:点击打开链接

也可以用微信调试工具进行调试:点击打开链接

libWecaht.php微信相关类文件

class LibWechat extends Base
{
    private $appid = '';
    private $appsecret = '';
    public $access_token; //微信全局
    public $error;
    public $code_ticket; //带参数二维码ticket
    public $scene_id; //带参数二维码scene_id(店铺ID)
    public $showqrcode; //带参数二维码图片路径

    /**
     * curl 请求
     */
    public function requestUrl($url, $data = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //禁止 cURL 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 为SSL不检查名称
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        if (!empty($data)) {

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $result = curl_exec($ch);

        curl_close($ch);
        return $result;
    }

    /**
     * 获取通用access_token
     */
    public function getAccessToken()
    {
        if (empty($_SESSION['accessToken'])) {
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->appsecret;
            $result = json_decode($this->requestUrl($url), true);
            if (empty($result['access_token'])) {
                $this->error = $result['errmsg'];
                return false;
            }
            $_SESSION['accessToken'] = $result['access_token'];
        }
        $this->access_token = $_SESSION['accessToken'];
        return true;
    }

    /**
     * 创建永久二维码ticket
     */
    public function getTicket($scene_id)
    {
        $this->scene_id = $scene_id;
        if (empty($this->access_token)) {
            $this->getAccessToken();
        }
        $ticket_array = '{
                         "action_name": "QR_LIMIT_SCENE",
                         "action_info": {
                             "scene": {
                                 "scene_id": "$this->scene_id",
                                 }
                             }
                         }';
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $this->access_token;
        $result = json_decode($this->requestUrl($url, $ticket_array), true);

        if (empty($result['ticket'])) {
            $this->error = $result['errmsg'];
            return false;
        }
        $this->code_ticket = $result['ticket'];
        //获取到ticket后,换取二维码路径图片
        $this->showqrcode();
        return $this->showqrcode;
    }

    /**
     * 创建永久二维码--换取二维码showqrcode
     */
    public function showqrcode()
    {
        if (empty($this->code_ticket)) {
            $this->error = "code_ticket不能为空";
            return false;
        }
        $showqrcode_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $this->code_ticket;
        $this->showqrcode = $showqrcode_url;
    }
}

wecaht.php调用获取二维码方法

    /**
     * 获取带参数二维码
     */
    public function actionQrCode()
    {
        $scene_id = Yii::$app->request->get('store_id');//scene_id,获取ticket必须字段
        if (empty($scene_id)) {
            $this->error = 'ID不能为空';
            return false;
        }
        $qr_code = new LibWechat();
        $qr_code_url = $qr_code->getTicket($scene_id);
        print_r($qr_code_url);
        die;
    }
这样你获取到的链接在浏览器中打开就能获取到二维码图片了

猜你喜欢

转载自blog.csdn.net/zhangzhangdan/article/details/80761564