获取二维码
发布于 5 年前 作者 xiaqiang 4563 次浏览 来自 问答

通过获取小程序页面二维码接口返回值中data如下图,怎样才可以展示成二维码图片

8 回复

有会弄的么

有没有直接调用的方法

在小程序内直接请求到返回的乱码无法保存成图片,要在后台进行请求。下面给出php的后台代码,传入要生成二维码页面的路径及参数,对其进行请求即可。

<?php

//传入要生成二维码的页面路径及参数

    $qpath = $_GET[‘qpath’];

     //获取access_token。填入自己小程序的appid和secret

    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=     &secret=     ";

    $html = file_get_contents($url);

    $back_prama = json_decode($html, true);

    //通过access_token获取二维码并临时保存在服务器

    $url2 = “https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$back_prama["access_token”];

    $post_data = array(

    “path” => $qpath,

    “width” => ‘430’

    );

    $data_string = json_encode($post_data);  

  $res = http_post_data($url2, $data_string);

 

  $result = file_put_contents(‘pic.png’,$res[1]);  

   

  function http_post_data($url, $data_string) {  

 

        $ch = curl_init();  

        curl_setopt($ch, CURLOPT_POST, 1);  

        curl_setopt($ch, CURLOPT_URL, $url);  

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(  

            ‘Content-Type: application/json; charset=utf-8’,  

            'Content-Length: ’ . strlen($data_string))  

        );  

        ob_start();  

        curl_exec($ch);  

        $return_content = ob_get_contents();  

        ob_end_clean();  

 

        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  

        return array($return_code, $return_content);  

    }

也可以直接base64处理一下直接显示在页面上

那 java 的代码应该怎么写

同样的需求,同问。

回到顶部