当前位置: 首页>编程语言>正文

php生成带参数小程序码

curl方式返回base64小程序码写入png文件

  • 获取access_token
    参考:微信文档-获取全局access_token
    https请求方式: GET
    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    (之前这里跟code换小程序access_token接口搞混了,大家引以为鉴)
   private function getAccessToken($appID, $appSerect) {
           $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appID."&secret=".$appSerect;
           // 时效性7200秒实现
           // 1.当前时间戳
           $currentTime = time();
           // 2.修改文件时间
           $fileName = "accessToken"; // 文件名
           // var_dump(is_file($fileName),$fileName);
           if(is_file($fileName)) {
               $modifyTime = filemtime($fileName);
               if(($currentTime - $modifyTime) < 7200) {
                   // 可用, 直接读取文件的内容
                   $accessToken = file_get_contents($fileName);
                   return $accessToken;
               }
           }
           // 重新发送请求
           $result = $this-> httpsRequest($url);
           $jsonArray = json_decode($result, true);
           // 写入文件
           $accessToken = $jsonArray['access_token'];
           file_put_contents($fileName, $accessToken);
           return $accessToken;
   }
  • 拿到access_token去请求接口
    access_token过期时间7200s,可以自己存文件或者存表,这里不做赘述

外部访问接口

 //获得小程序码
    public function get_xcx_qrcode() {
        $db = DBAction::getInstance();
        $request = $this->getContext()->getRequest();
        $openid = self::checkOpenid();
        if(!$openid){
            $data['msg'] = '缺少参数'.time();
            $data['data']['qcode'] = [];
            $data['status'] = 0;
            echo json_encode($data);exit();
        };
        $user_obj = $db->select("SELECT user.id, user.wx_id,code.* FROM lkt_user as user LEFT JOIN lkt_xcx_qcode as code ON user.id=code.user_id WHERE user.wx_id='$openid' ");
        $user_arr = $user_obj?$this->objectToArray($user_obj):false;
        if(!$user_arr){
            $data['msg'] = '没有用户数据';
            $data['status'] = 1;
            $data['data']['qcode'] = [];
        }else{
            $user_id =  $user_arr['0']['id'];
            $xcx_qcode =  $user_arr['0']['xcx_qcode'];
            $qcode_url =  $user_arr['0']['qcode_url'];
            if($xcx_qcode){
                $data['msg'] = '小程序码返回';
                $data['status'] = 1;
                $data['data']['qcode'] = $qcode_url;
            }  else {
                //这里业务逻辑返回小程序码链接
                $qcode_url= $this->returnQcode($user_id, $user_arr['0']['wx_id']);
                if($qcode_url){
                    $data['data']['qcode'] = $qcode_url;
                    $data['status'] = 1;
                    $data['msg'] = '小程序码生成返回';
                }else{
                    $data['data']['qcode'] = '';
                    $data['status'] = 2;
                    $data['msg'] = '异常错误';
                }
            }
        }
       echo json_encode($data);exit();
    }
    private function checkOpenid(){
        $db = DBAction::getInstance();  
        $request = $this->getContext()->getRequest();
        if($request->getParameter('openid')){
           $openid = addslashes(trim($request->getParameter('openid')));
           return $openid;
        }  else {
            return FALSE;
        }
    }
    
  • 获取和保存小程序码的参考
    先查看是否已有,有的直接返回,没有的去获取写入png,写入数据库
 **tips:不管控制器在哪,$path = "./xxx.txt"; 文件在入口文件目录下
小程序码不需要参数的话接口是
https://api.weixin.qq.com/wxa/getwxacode?access_token=$your_access_token;
需要参数的话,是0
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$your_access_token;
  // 在线返回小程序码
    public function returnQcode($user_id,$openid){
        $db = DBAction::getInstance();
        $sql = "select * from lkt_config where id=1";
        $r = $db->select($sql);
        if($r){
            $appid = $r[0]->appid; // 小程序唯一标识
            $appsecret = $r[0]->appsecret; // 小程序的 app secret
            $access_token =  self::getAccessToken($appid,$appsecret);
            $uri = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
//参数 scene  做邀请标识了,可以多个参数,接收的时候在定义的path页接收处理即可
//width 参数最小280 低于280的结果会等于280,
            $parameter =[
                    "path"=>  "pages/index/index",
                    "width"=>430,
                    "scene"=>$user_id,
                ];
            $jsonParameter = json_encode($parameter);
            $qcodeData = $this->httpsRequest($uri,$jsonParameter);
            if(!is_object($qcodeData)){
                $codeName = $openid.'.png';
                $path = $this->imgGetUrl().'xcxcode/'.$codeName;
                $newFilePath = './images/xcxcode/'.$codeName;
                $fileStream = fopen($newFilePath,"w");
                if(fwrite($fileStream,$qcodeData)){
                    fclose($fileStream);
                    $sql = "insert into lkt_xcx_qcode(user_id,xcx_qcode,qcode_url) values('$user_id','$codeName','$path')";
                    if($db->insert($sql)){
                        return $path;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }  else {
                return false;
            }
        }  else {
           return false; 
        }
    }

curl参考

 public function httpsRequest($url, $data=null) {
        // 1.初始化会话
        $ch = curl_init();
        // 2.设置参数: url + header + 选项
        // 设置请求的url
        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, true);
        if(!empty($data)) {
            // 发送post请求
            curl_setopt($ch, CURLOPT_POST, 1);
            // 设置发送post请求参数数据
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        // 3.执行会话; $result是微信服务器返回的JSON字符串
        $result = curl_exec($ch);
        // 4.关闭会话
        curl_close($ch);
        return $result;
    }

https://www.xamrdz.com/lan/5x31848554.html

相关文章: