收藏
回答

小程序二维码生成接口返回图片有问题

API/组件名称 终端类型 微信版本 基础库版本
获取二维码 客户端 所有 所有

调用官方生成二维码接口,返回的图片有问题。

通过客户端方式调用:(部分代码)

var token = res.data.access_token;
                    wx.request({
                        url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + token,
                        method: 'POST',
                        data: {
                            scene: 'orderId=' + orderId,
                            path: 'pages/hall/receiveDetail/receiveDetail',
                            width: 258,
                            auto_color: false,
                            line_color: { "r": "0", "g": "0", "b": "0" }
                        },
                        dataType:'utf-8',
                        success: function (res) {
                            callback(res.data);
                        }
                    })

返回的图片为乱码,经过php处理后,基本上为不可见的图片

通过php方式调用:

//获取access_token
    public function get_access_token(){
        $appid = $wx_config['appid'];
        $secret = $wx_config['secret'];
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        return $data = $this->curl_get($url);
    }
 
    public function curl_get($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $data;
    }
    //获得二维码
    public function get_qrcode() {
        header('content-type:image/jpeg');
        $data = array();
        $data['scene'] = "orderId=14915621621515756124648";
        $data['page'] = "pages/hall/receiveDetail/receiveDetail";
        $data = json_encode($data);
        $access = json_decode($this->get_access_token(),true);
        $access_token= $access['access_token'];
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
        $da = $this->get_http_array($url,$data);
 
        echo json_encode(array('pictures'=>$da));
        $this->assign('data',$da);
        $this->fetch();
 
    }
    public function get_http_array($url,$post_data) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出,不用print_r();也会在后面多个1
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        $out = json_decode($output);
        return $out;
    }

返回的结果一样,请求官方能给予解答。

回答关注问题邀请回答
收藏

2 个回答

  • 九歌^
    九歌^
    2018-04-24

    此问题是 access_token失效的问题。

    2018-04-24
    有用
    回复
  • 九歌^
    九歌^
    2018-03-07

    相同的问题,请问解决了吗?是偶现的问题。

    2018-03-07
    有用
    回复
登录 后发表内容