PHP获取小程序码,小程序带参数跳转 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 小程序相关 >

PHP获取小程序码,小程序带参数跳转

发表时间:2020-11-20

发布人:葵宇科技

浏览次数:32

PHP获取小程序码,小程序带参数跳转

获取小程序码首先需要获取Accesstoken ,下面是获取Accesstoken,注意超时时间所有我把时间往前提了一下,生成小程序码的时候先调取获取Accesstoken的方法

 //获取accesstokn 并保存
    public function getAccessToken(){

        //$file_path = VENDOR_PATH.'wchat/access_token';
        $file_path = '../application/common/access_token';

        if(is_file($file_path)){
            $connect = file_get_contents($file_path);
            if($connect!=''){
                $ret = json_decode($connect,true);
                //已存在
                if($ret['access_token'] && time()-$ret['time'] <7000){
                    $this->access_token = $ret['access_token'];
                    return true;
                }
            }
        }

        $wx_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->wxAppId}&secret={$this->wxAppSecret}";
        $ret = httpUtil($wx_url);
        $ret = json_decode($ret,true);
        if($ret['access_token']){
            $data = array(
                'access_token'=>$ret['access_token'],
                'time'=>time()
            );
            file_put_contents($file_path, json_encode($data));
            $this->access_token = $ret['access_token'];
            return true;
        }
    }
//获取小程序码
    public function getQrCode($openid){
        $this->getAccessToken();//更新accesstoken
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$this->access_token";//官方获取小程序码url
        $data['page']='pages/index/index';//小程序的跳转页面地址
        $data['scene'] = "$openid";  //请求参数
       
        $res = httpUtil($url,json_encode($data),'POST');
        if(!empty($res['errcode'])){
            return false;
        }
        $file ="qrcode/$openid.jpg";
        file_put_contents('./'.$file,$res);
        if (file_exists($file)) {
            //return $_SERVER ['HTTP_HOST'].'/'.$file;
            return '/'.$file;
        }else{
            return false;
        }
    }

相关案例查看更多