php生成微信菊花码 小程序码

该代码实现了一个用于生成微信小程序二维码的类,包括获取access_token、处理错误、生成不同类型的二维码(菊花码和普通二维码)并进行图片合成的功能。同时,类中包含了异常处理和HTTP请求方法。
<?php
// +----------------------------------------------------------------------
// | ITUX.CN [ ITUX.CN软件定制开发 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2023 https://itux.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 此软件并不是自由软件,未经许可不能去掉ITUX相关版权
// +----------------------------------------------------------------------
// | Author: 13103895694 <itholiday@126.com>
// +----------------------------------------------------------------------

namespace addons\gaga\library\cqrs\libs;

use think\exception\ValidateException;

class Wxaqrcode
{
   
   
    protected $wxapp_settings;
    protected $default_wxapp_settings;
    private $appId;
    private $appSecret;
    private $access_token;
    private $error = '';

    public function __construct($app_id,$app_secret)
    {
   
   
        if (empty($app_id) || empty($app_secret)) throw new \think\Exception('小程序参数错误');
        $this->appId = $app_id;
        $this->appSecret = $app_secret;
        $access_token = \think\Cache::get('wxa:access_token:' . $this->appId);
        if (!$access_token) {
   
   
            $target_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appId . "&secret=" . $this->appSecret;
            $res = $this->http_get($target_url);
            $res = json_decode($res, true);
            if (isset($res['errcode']) && $res['errcode'] != 0) {
   
   
                $content = '';
                switch (!$res || $res['errcode']) {
   
   
                    case 40001:
                        $content = "AppSecret错误或者AppSecret不属于这个小程序,请开发者确认AppSecret的正确性";
                        break;
                    case 40164:
                        $content = "调用接口的IP地址不在白名单中,请在接口IP白名单中进行设置";
                        break;
                }
                throw new ValidateException($content);
            }
            $access_token = $res['access_token'];
            $expire = $res['expires_in'] ? intval($res['expires_in']) : 7200;
            \think\Cache::set('wxa:access_token:' . $this->appId, $res['access_token'], $expire);
        }
        $this->access_token = $access_token;
    }

    //菊花码,适用于大量生成二维码,注意scene最大32个可见字符
    public function getWxQrcode($page = 'pages/index/index', $scene = '', $width = 430, $filepath = null, $qrtype 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值