您好,欢迎访问一九零五行业门户网

PHP生成带参数的微信小程序的二维码

这篇文章主要介绍了关于php生成带参数的微信小程序的二维码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
微信小程序官方开放了3个创建二维码的接口,其中有一个是生成二维码的,还有一个是葵花状的小程序码,我这里就用php生成二维码。
首先要获取access_token这个请求起来也是很容易的,微信开发文档有请求接口:
要把自己的小程序的appid和appsecret获取到
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=appsecret
access_token只有2小时有效期,所以要缓存最好,避免重复请求
构建请求参数
可以构建一个数组,然后转成json数据赋给一个变量
$param = json_encode(array(path=>pages/index/index?id=123,width=> 150));
然后post数据发送到微信服务器换取二维码
完整代码<?phpheader('content-type:text/html;charset=utf-8');//配置appid、appsecret$appid = "appid"; $appsecret = "appsecret"; //获取access_token$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";//缓存access_token session_start(); $_session['access_token'] = ""; $_session['expires_in'] = 0; $access_token = ""; if(!isset($_session['access_token']) || (isset($_session['expires_in']) && time() > $_session['expires_in'])) {     $json = httprequest( $access_token );     $json = json_decode($json,true);      // var_dump($json);     $_session['access_token'] = $json['access_token'];     $_session['expires_in'] = time()+7200;     $access_token = $json[access_token];  }  else{     $access_token =  $_session[access_token];  }//构建请求二维码参数//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx//width是二维码宽度$qcode =https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$access_token;$param = json_encode(array(path=>pages/index/index?id=123,width=> 150));//post参数$result = httprequest( $qcode, $param,post);//生成二维码file_put_contents(qrcode.png, $result);$base64_image =data:image/jpeg;base64,.base64_encode( $result );//把请求发送到微信服务器换取二维码  function httprequest($url, $data='', $method='get'){    $curl = curl_init();      curl_setopt($curl, curlopt_url, $url);      curl_setopt($curl, curlopt_ssl_verifypeer, 0);      curl_setopt($curl, curlopt_ssl_verifyhost, 0);      curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']);      curl_setopt($curl, curlopt_followlocation, 1);      curl_setopt($curl, curlopt_autoreferer, 1);      if($method=='post')    {        curl_setopt($curl, curlopt_post, 1);         if ($data != '')        {            curl_setopt($curl, curlopt_postfields, $data);          }    }    curl_setopt($curl, curlopt_timeout, 30);      curl_setopt($curl, curlopt_header, 0);      curl_setopt($curl, curlopt_returntransfer, 1);      $result = curl_exec($curl);      curl_close($curl);      return $result;  } ?>
使用方法:1、新建qrcode.php
2、拷贝上面代码进去
3、修改appid和appsecret
4、访问qrcode.php
即可生成一个带参数的小程序二维码
但是这个接口生成的是有logo的小程序二维码。
我们想要生成一个简简单单的,无logo的小程序二维码可以吗?
官方的是没有办法的,但是我们可以用第三方的接口生成普通的二维码的方式。
生成无logo二维码步骤1、先解码,把刚才生成的小程序二维码解码,获得url
2、把获得的url用普通的二维码生成接口生成普通的无logo的二维码
1、解码解码其实有很多的库我这里直接用了第三方的jssdk解码,免费的,需要申请接口和appid和appkey
<h2>生成无logo二维码</h2><!doctype html><html>  <head>    <title>php生成微信小程序二维码</title>    <meta http-equiv="content-type" content="text/html; charset=utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>    <script src="hprose.min.js"></script>    <script src="md5.min.js"></script>  </head>  <body>    <!--显示二维码-->    <p id="show_test"></p>    <p id="select_img" style="display:none;">        <input type="file" id="imgtest" type="file" accept=".gif,.jpg,.jpeg,.png">        <br/>        <img src="" id="showimage" alt="">    </p>    <a href="javascript:;" id="qrdecode">点击生成无logo二维码</a>    <script>    var get_timestamp = function(){        var timestamp =0;        timestamp = date.parse(new date());// 获取当前时间戳(以s为单位)        timestamp = timestamp / 1000;        return timestamp;    };    //config,申请解码接口:http://www.wwei.cn/,免费    var api_id = 'xxxxxxx';//改为您自己的    var api_key = 'xxxxxxxxx';//改为您自己的    var timestamp = get_timestamp();    var client = hprose.client.create('http://hprose.wwei.cn/qrcode.html', ['qrencode','qrdecode']);    //解码    $(#qrdecode).click(function(){            var timestamp = get_timestamp();            var imgurl = '';//远程图片            var imgdata ='<?php echo $base64_image;?>';//本地图片,直接调用生成的官方小程序二维码,用于解码            var signature = md5(api_key + timestamp + imgurl + imgdata);            client.ready(function(qrcode) {                qrcode.qrdecode(api_id,signature,timestamp,imgurl,imgdata)                .then(function(result) {                    if(result.status !=1){                        alert(result.msg);                        return ;                    }                    //生成无logo二维码api接口                    var qrcodeapi = http://qr.liantu.com/api.php?text=;                    //拼接接口+解码url并输出为图片                    $(#show_test).html('<img src="'+qrcodeapi+result.data.raw_text+'"/>');                },function(e) {                    console.error(e);                });            },            function(e) {                console.error(e);            });    });    </script>  </body></html>
上面是通过js来实现的解码,解码还是调用了php生成的二维码的一个图片地址的
<?php echo $base64_image;?>
上面是调用本地图片,所以需要结合php生成小程序二维码的代码使用这个解码程序。
解码后需要生成无logo的二维码,这个我只需要调用一个接口就行。
接口:http://qr.liantu.com/api.php?...
然后拼接解码后的url就可以生成一个二维码了。
那么生成小程序码和生成无logo二维码的代码结合起来就是:
<?phpheader('content-type:text/html;charset=utf-8');//配置appid、appsecret$appid = "appid"; $appsecret = "appsecret"; //获取access_token$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";//缓存access_token session_start(); $_session['access_token'] = ""; $_session['expires_in'] = 0; $access_token = ""; if(!isset($_session['access_token']) || (isset($_session['expires_in']) && time() > $_session['expires_in'])) {     $json = httprequest( $access_token );     $json = json_decode($json,true);      // var_dump($json);     $_session['access_token'] = $json['access_token'];     $_session['expires_in'] = time()+7200;     $access_token = $json[access_token];  }  else{     $access_token =  $_session[access_token];  }//构建请求二维码参数//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx//width是二维码宽度$qcode =https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$access_token;$param = json_encode(array(path=>pages/index/index?id=123,width=> 150));//post参数$result = httprequest( $qcode, $param,post);//生成二维码file_put_contents(qrcode.png, $result);$base64_image =data:image/jpeg;base64,.base64_encode( $result );//把请求发送到微信服务器换取二维码  function httprequest($url, $data='', $method='get'){    $curl = curl_init();      curl_setopt($curl, curlopt_url, $url);      curl_setopt($curl, curlopt_ssl_verifypeer, 0);      curl_setopt($curl, curlopt_ssl_verifyhost, 0);      curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']);      curl_setopt($curl, curlopt_followlocation, 1);      curl_setopt($curl, curlopt_autoreferer, 1);      if($method=='post')    {        curl_setopt($curl, curlopt_post, 1);         if ($data != '')        {            curl_setopt($curl, curlopt_postfields, $data);          }    }    curl_setopt($curl, curlopt_timeout, 30);      curl_setopt($curl, curlopt_header, 0);      curl_setopt($curl, curlopt_returntransfer, 1);      $result = curl_exec($curl);      curl_close($curl);      return $result;  } ?><h2>生成小程序官方二维码</h2><!-- 生成小程序官方二维码 --><br/><h2>生成无logo二维码</h2><!-- 生成无logo二维码 --><!doctype html><html>  <head>    <title>php生成微信小程序二维码</title>    <meta http-equiv="content-type" content="text/html; charset=utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>    <script src="hprose.min.js"></script>    <script src="md5.min.js"></script>  </head>  <body>    <p id="show_test"><!--显示信息--></p>    <p id="select_img" style="display:none;">        <input type="file" id="imgtest" type="file" accept=".gif,.jpg,.jpeg,.png">        <br/>        <img src="" id="showimage" alt="">    </p>    <a href="javascript:;" id="qrdecode">点击生成无logo二维码</a>    <script>    var get_timestamp = function(){        var timestamp =0;        timestamp = date.parse(new date());// 获取当前时间戳(以s为单位)        timestamp = timestamp / 1000;        return timestamp;    };    //config,申请解码接口:http://www.wwei.cn/,免费    var api_id = 'xxx';//改为您自己的    var api_key = 'xxx';//改为您自己的 (实际使用,建议在后台完成 signature 签名,以防暴露 api_key ,或定期更改)    var timestamp = get_timestamp();    var client = hprose.client.create('http://hprose.wwei.cn/qrcode.html', ['qrencode','qrdecode']);    //解码    $(#qrdecode).click(function(){            var timestamp = get_timestamp();            var imgurl = '';//远程图片            var imgdata ='<?php echo $base64_image;?>';//本地图片,直接调用生成的官方小程序二维码,用于解码            var signature = md5(api_key + timestamp + imgurl + imgdata);            client.ready(function(qrcode) {                qrcode.qrdecode(api_id,signature,timestamp,imgurl,imgdata)                .then(function(result) {                    if(result.status !=1){                        alert(result.msg);                        return ;                    }                    //生成无logo二维码api接口                    var qrcodeapi = http://qr.liantu.com/api.php?text=;                    //拼接接口+解码url并输出为图片                    $(#show_test).html('<img src="'+qrcodeapi+result.data.raw_text+'"/>');                },function(e) {                    console.error(e);                });            },            function(e) {                console.error(e);            });    });    </script>  </body></html>
但是解码需要用到的两个js库
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php大牛分享:php代码编写的规范,总结的很全
对于php面向对象设计五大原则(solid)的总结
以上就是php生成带参数的微信小程序的二维码的详细内容。
其它类似信息

推荐信息