php 生成小程序二维码

<?php
   
   
/**
     *  生成微信二维码
     *
     */
    function wxQRcode(){
        //code为参数
        $appid = '';//小程序appid
        $secret = '';//小程序secret
        $zhi = '';//参数
//        $code = '2';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        //开启session
        session_start();
        // 保存2小时
        $lifeTime = 2 * 3600;
        setcookie(session_name(), session_id(), time() + $lifeTime, "/");
        // echo $url;
//        $access_token = $_SESSION['access_token'];
        if(empty($access_token)){
            $access_token_data = $this->getJson($url);
            $access_token = $access_token_data['access_token'];
            $_SESSION['access_token'] = $access_token;
        }
        if(!empty($access_token)){
            $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
            $data['path'] = '';//小程序地址 'pages/index?字段='.$值
            $data['scene'] = 'type=qrcode';//(string类型,必须是数字)
            $data['width'] = 430;
            $result = $this->curlPost($url,$data,'POST');
            // p($result);
            $filename = md5($zhi);  //文件名
            $ret = file_put_contents('./uploads/'.$filename.'.png', $result, true);
            $path='/uploads/'.$filename.'.png';




            $data = [
                'codeUrl' => '',//小程序二维码
//                'coverUrl' => "".'/uploads/广告图.jpg',//题库图片
                'headUrl' => '',//头像
//                'bgUrl' => .'/uploads/背景图.jpg',//图片背景填充,和 fillStyle 只能传一个,bgUrl 优先级高于 fillStyle
                'fillStyle' => '#0688ff',//纯色背景填充颜色
                'nickName' => '',//授权登录的用户名
                'miniName' => '',//小程序名称
            ];
            $this->success('生成小程序二维码成功',$data);
//            echo '成功';
        }else{
            $this->error('生成小程序二维码失败');
        }
    }

    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);
    }

    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;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成小程序二维码可以通过微信官方提供的API实现,而PHP可以通过调用这个API来生成小程序二维码。具体步骤如下: 1. 获取access_token:调用微信官方提供的获取access_token的API,获取access_token。注意:access_token需要缓存,不要频繁调用该API获取。 2. 调用生成小程序二维码的API:调用微信官方提供的生成小程序二维码的API,传入必要的参数,比如path、width等。 3. 保存小程序二维码:把API返回的二进制流保存为图片即可。 下面是示例代码: ```php <?php $app_id = '你的小程序appid'; $app_secret = '你的小程序appsecret'; //获取access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$app_secret}"; $res = file_get_contents($url); $res = json_decode($res, true); $access_token = $res['access_token']; //调用生成小程序二维码的API $path = '/pages/index/index'; $width = 430; $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$access_token}"; $data = array( 'path' => $path, 'width' => $width ); $data = json_encode($data); $options = array( 'http' => array( 'header' => "Content-type:application/json", 'method' => 'POST', 'content' => $data, 'timeout' => 60 ) ); $context = stream_context_create($options); $res = file_get_contents($url, false, $context); //保存小程序二维码 file_put_contents('./qrcode.jpg', $res); ``` 需要注意的是,生成小程序二维码的API有一些限制,比如只能生成已发布的小程序二维码有效期为30天等。具体详情可以参考微信官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值