微信小程序官方開放了3個建立二維碼的接口,其中有一個是生成二維碼的,還有一個是葵花狀的小程序碼,我這裏就用php生成二維碼。javascript
這個請求起來也是很容易的,微信開發文檔有請求接口:
要把本身的小程序的APPID和APPSECRET獲取到php
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
access_token只有2小時有效期,因此要緩存最好,避免重複請求css
能夠構建一個數組,而後轉成json數據賦給一個變量html
$param = json_encode(array("path"=>"pages/index/index?id=123","width"=> 150));
而後POST數據發送到微信服務器換取二維碼java
<?php header('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; } ?>
一、新建qrcode.php
二、拷貝上面代碼進去
三、修改APPID和APPSECRET
四、訪問qrcode.phpjson
便可生成一個帶參數的小程序二維碼小程序
可是這個接口生成的是有LOGO的小程序二維碼。
咱們想要生成一個簡簡單單的,無LOGO的小程序二維碼能夠嗎?
官方的是沒有辦法的,可是咱們能夠用第三方的接口生成普通的二維碼的方式。微信小程序
一、先解碼,把剛纔生成的小程序二維碼解碼,得到URL
二、把得到的URL用普通的二維碼生成接口生成普通的無LOGO的二維碼api
解碼其實有不少的庫我這裏直接用了第三方的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> <!--顯示二維碼--> <div id="show_test"></div> <div id="select_img" style="display:none;"> <input type="file" id="imgTest" type="file" accept=".gif,.jpg,.jpeg,.png"> <br/> <img src="" id="showImage" alt=""> </div> <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?...
那麼生成小程序碼和生成無LOGO二維碼的代碼結合起來就是:
<?php header('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> <!-- 生成小程序官方二維碼 --> <img src="<?php echo $base64_image;?>"/> <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> <div id="show_test"><!--顯示信息--></div> <div id="select_img" style="display:none;"> <input type="file" id="imgTest" type="file" accept=".gif,.jpg,.jpeg,.png"> <br/> <img src="" id="showImage" alt=""> </div> <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庫
這裏直接提供下載地址:https://pan.baidu.com/s/1UYDj...
做者:TANKING2018-7-13學習交流微信:face6009