记录PHP 生成小程序二维码

这篇博客介绍了如何在PHP后台生成微信小程序的二维码图片。首先,定义了一个生成唯一标识的函数,然后通过调用微信API获取access_token。接着,使用curl发送POST请求生成小程序二维码,并将结果保存为PNG图片。整个过程涉及了HTTP请求、JSON解析以及文件操作等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHP 后台生成小程序二维码图片

<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Treasure extends Model
{
    //生成唯一标识
    public static function guid($factor='',$prefix='',$suffix=''){
        list($usec, $sec) = explode(" ", microtime());
        $guid = $prefix. $factor. $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']
            . $sec . $usec
            . mt_rand(0,1000000).time(). mt_rand(0,1000000).$suffix;
        $guid = substr(sha1($guid),8,32);
        $guid = base_convert($guid,16,36);
        return $prefix.$guid.$suffix;
    }
    //获取微信生成二维码 id(可以是调转小程序参数,也可以是唯一标识)  JumpUrl(小程序跳转路径)
    public  static function wx_code($id,$JumpUrl){
        $appid ='appid';
        $secret ='AppSecret';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        //开启session
        // 保存2小时
        $lifeTime = 2 * 3600;
        setcookie(session_name(), session_id(), time() + $lifeTime, "/");
        if(empty($access_token)){
            $access_token_data = self::getJson($url);
            $access_token = $access_token_data['access_token'];
            $_SESSION['access_token'] = $access_token;
        }
        $access_token = $_SESSION['access_token'];
        if(!empty($access_token)){
            $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
//            $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
            $data['path'] = $JumpUrl;
            $data['scene'] =$id;//(接口为getwxacodeunlimit使用,传递小程序参数)
            $data['width'] = 430;
            $result = self::curlPost($url,$data,'POST');
            $str = uniqid(mt_rand(),1);
            $file='/uploads/qr_code/'.date('Ymd',time()).'/';
            $file2=$_SERVER['DOCUMENT_ROOT'].$file;
            if(!is_dir($file2))//检测目录是否存在
            {
                mkdir($file2,0777,true);
            }
            file_put_contents($file2.$id.md5($str).'.png', $result, true);
            return  $file.$id.md5($str).'.png';
        }
    }

    public static   function getJson($url,$data=array(),$method='GET'){
        $ch = curl_init();//1.初始化
        curl_setopt($ch, CURLOPT_URL, $url);//2.请求地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
        //4.参数如下
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if($method=="POST"){//5.post方式的时候添加数据
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

    public static  function curlPost($url,$data,$method){
        $ch = curl_init();   //1.初始化
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
        //4.参数如下
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

        if($method=="POST"){//5.post方式的时候添加数据
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);//6.执行

        if (curl_errno($ch)) {//7.如果出错
            return curl_error($ch);
        }
        curl_close($ch);//8.关闭
        return $tmpInfo;
    }
    

    







}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是誰萆微了承諾

你的鼓励是对我最大的支持

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

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

打赏作者

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

抵扣说明:

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

余额充值