php经过微信接口获取小程序二维码

modeljson

private    $i          = 0;
public function getAccess_token($appid,$secret){
    $reqUrl     = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
    // crossDomain  curl执行post请求,网上查找便可
    $ref        = crossDomain($reqUrl);
    $re         = json_decode($ref,true);
    if($re['errcode'] != 0){
        $this->i++;
        if($this->i < 4){
            echo '请求失败,稍后重试';die;
        }
        $this->getAccess_token($appid,$secret);
    }

    return $ref;
}
public function getWxaCodeUnlimit($access_token){
    $reqUrl     = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
    return      $reqUrl;
}

controller小程序

public function qrCode(){
        //格式自选,不一样格式貌似加载速度略有不一样,想加载更快可选择jpg
    //        header('content-type:image/gif');
    //        header('content-type:image/png');
            header('content-type:image/jpg');

        $ID      = trim($this->input->get('ID'));
        if(!$ID || !is_numeric($ID)){
            $errorArr   = array('errcode'=>-3,'errmsg'=>'illegal request');
            echo  json_encode($errorArr,JSON_UNESCAPED_UNICODE);
            die;
        }

        $this->load->model('Wxapi_model', 'wxApi');
        //小程序
        $appId    = '78897';
        $appSecret = '78998645';
        $access_token   = $this->cache->get('access_token');
        if(!$access_token){
            $access_token = $this->wxApi->getAccess_token($appId,$appSecret);
            $this->cache->save('el:access_token',$access_token,(60*60*2-5));
        }
        $access_token   = json_decode($access_token,true);

        $getWxaCode   = $this->wxApi->getWxaCodeUnlimit($access_token['access_token']);

        //参数  page  =》 不是指服务端生成小程序码接口,指的是小程序调用这个接口时的js文件路径
        $data = array(
            'scene'             =>'ID='.$ID,
            'page'              =>'pages/index/index',
//            'page'              =>'',
            'width'             =>20,
//            'auto_color'        =>false,
        );

        $data = json_encode($data);

        //post   curl执行post请求,网上查找便可
        echo  post($getWxaCode, $data);

    }
相关文章
相关标签/搜索