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

model

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);

    }

转载于:https://my.oschina.net/u/3268486/blog/1826728

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值