PHP獲取小程序碼,小程序帶參數跳轉 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 小程序相關 >

PHP獲取小程序碼,小程序帶參數跳轉

發表時(shí)間:2020-11-20

發布人(rén):融晨科技

浏覽次數:46

PHP獲取小程序碼,小程序帶參數跳轉

獲取小程序碼首先需要(yào / yāo)獲取Accesstoken ,下面是(shì)獲取Accesstoken,注意超時(shí)時(shí)間所有我把時(shí)間往前提了(le/liǎo)一下,生成小程序碼的(de)時(shí)候先調取獲取Accesstoken的(de)方法

 //獲取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);
                //已存在(zài)
                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';//小程序的(de)跳轉頁面地(dì / de)址
        $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;
        }
    }

相關案例查看更多