微信小程序生成指定页面的小程序码

2020-05-18  本文已影响0人  端木安玉

问题: 小程序码默认只有一个到首页的二维码

发布时候指定的首页的一个二维码。但是在运营过程中通常会指定多个入口,这样需要我们提供多个直达页面的二维码。

解决:前端自己生成

准备材料
小程序appid
小程序密钥secret
一个发请求工具 这里选postman
动手
1 查看官方文档获取access_token

2 生成二维码接口文档

3 打开postman调用接口(结果如下图)

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的小程序APPID&secret=你的小程序密钥
获取access_token示例

image.png

最后一步调用接口生成二维码
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=上一个接口获取的access_token
body参数: 参数说明

image.png

{"scene":"a=100","page":"pages/gift/main","width":200,"auto_color":false,"line_color":{"r":"0","g":"0","b":"0"}}
1
接口示例


image.png

这样指定的二维码就生成了, page参数可以任意自己指定(当然是要已发布的页面)。

注:

返回值说明

如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。

因为返回的是二进制的图片所以要进行图片转换

php 获取小程序二维码返回的 Buffer二进制数据 保存图片 全套代码

 $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token; //$access_token为自己获取的token,后面有写
    
     $codeinfo = $this->http_request($url, $array_json);//图片流 buffer  curl提交数据 获取图片流
    
    //判断是否是 json格式
    if(is_null(json_decode($codeinfo))){
        //不是json数据   有数据流  json_decode($codeinfo)返回值为 null
        
        $jpg = $codeinfo;//得到post过来的二进制原始数据
         $file = fopen("xcxcode/wxcode1.jpg","w");//创建件准备写入,文件名xcxcode/wxcode1.jpg为自定义
        fwrite($file,$jpg);//写入
        fclose($file);//关闭
    }else{
        //是json数据
        $codeinfo_array=json_decode($codeinfo,true);
        //没有接收到数据流  
        return "no";
    }

后台php生成代码

// 生成小程序码
public function wxacodeun(){

        //获取token
        $token = $this->getactionGetToken();
        //设置url
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$token;

        //设置信息

        $deid =input('deid');
        //设置发送的消息
        $message = [
            'scene'=> $deid,// 用户openid
            'page'=>'designdetail/designdetail',//模板id

        ];

        $data = json_encode($message);

        //发送
        $ressend = $this->actionCurlRequest($url,$data);
        $routename = "static/design/".time().".jpg";
        $jpg = $ressend;//得到post过来的二进制原始数据
        $file = fopen($routename,"w");//创建件准备写入,文件名xcxcode/wxcode1.jpg为自定义
        fwrite($file,$jpg);//写入
        fclose($file);//关闭
        echo "<img src="."/".$routename.">";
        die;
    }
 // 获取令牌
    private function getactionGetToken(){
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3cbbfbf6d04b7e45&secret=a7521077614d10d2860afd39c3ef23c9';
        $res = json_decode($this->actionCurlRequest($url));
        return $res->access_token;
    }
    //curl请求,支持post和get
    private function actionCurlRequest($url,$data=null){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
        if(!empty($data)){
            curl_setopt($curl,CURLOPT_POST,1);
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }

        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
上一篇 下一篇

猜你喜欢

热点阅读