PHP 微信小程序太阳码

     //获取小程序码
     public function getQrCode(){
         
        header('Content-Type: text/html; charset=utf-8'); //网页编码
      
        $this->getAccessToken();
        
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$this->access_token";
        $data['page']='pages/jihuo/index';//页面路径
        $data['scene'] = '1';//自定义参数  只能是数字
        $res = $this->httpUtil($url,json_encode($data),'POST');

        if(!empty($res['errcode'])){
            return $this->renderError($model->getError() ?: '下载失败');
        }

        $file ="../web/member/".$batch;

        if (!is_dir($file)) {
            mkdir($file,0700);
        }

        file_put_contents($file.'/'.$CardNumber.".jpg",$res);
        if (file_exists($file)) {

            return True;

        }else{
            return $this->renderError($model->getError() ?: '下载失败');
        }


    }

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

        $file_path = '../source/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=你的APPID&secret=你的secret";
        $ret = $this->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 httpUtil($url, $data = ''){
        $ch = curl_init();
        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, 1);
        if ($data != '') {
        	curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

	

	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值