PHP生成微信小程序太阳码

先获取微信的接口调用凭证

function accessToken($appId, $appSecret)
{
    $tokenFile = 'access_token.json';// 保存的access_token
    $data = json_decode(file_get_contents($tokenFile));
    if ($data->expire_time < time()) {
        // 调用微信接口获取access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
        $result = json_decode($result, true);
        $accessToken = $result['access_token'];
        if ($accessToken) {
            $data->expire_time = time() + 7000;// access_token的有效时间为2个小时,可以少设置一点时间
            $data->access_token = $accessToken;
            file_put_contents($tokenFile, json_encode($data));// 写入文件
        }
    } else {
        $accessToken = $data->access_token;
    }
    return $accessToken;
}

其中的access_token.json是保存凭证的文件,token有两个小时的有效期,可以保存下来反复使用,保存方法可以存缓存或者存数据库,按自己的习惯来即可

{"expire_time":0,"access_token":"access_token"}

最后调用微信接口生成太阳码并保存

/**
 * 生成小程序太阳码
 * @param $path string 小程序路径
 * @param $filepath string 生成图片文件夹
 * @param $filename string 生成图片名
 * @param $scene string 参数
 * @param int $width 生成图片大小
 * @return string
 */
function sunCode(string $filepath, string $filename, string $scene, string $path, int $width = 430): string
{
    $appId = "你的appId";
    $appSecret = "你的appSecret";
    $accessToken = accessToken($appId,$appSecret);
    $postData = array(
        "page" => $path,
        "scene" => $scene,
        "width" => $width,
        "env_version" => "release",// 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
        "is_hyaline" => true,// 默认是false,是否需要透明底色,为 true 时,生成透明底色的小程序(需要拼接到海报上可用此参数)
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 500);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
    $exec  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if (empty($error)) {
        file_put_contents($filepath . $filename, $exec);
        return $filepath . $filename;
    }else{
        return "";
    }
}
$filepath = "uploads/";// 保存的路径
$filename = rand(100000, 999999) . '.png';// 图片名称
$scene = 'params';// 太阳码携带的参数
$path = 'pages/index/index';// 需要跳转的小程序页面路径
$image = sunCode($filepath, $filename, $scene, $path);
echo $image;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

火³可²

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

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

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

打赏作者

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

抵扣说明:

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

余额充值