今天get到了一个小程序基本技能,就是生成一个带参数的小程序二维码,小程序的二维码和我们平时直接套网址生成的二维码本质上没有区别 都是一个链接地址,奈何 不知道人家小程序存放的地址呢,所以,想生成能直接一扫就进入小程序并且携带参数,就得按照人家的规定来。
项目需求:用户二维码推广,每推广一个新用户,需要给推广用户一笔推广费,所以,二维码里要带上当前用户的id。
作为一个小白,听到这个需求 我当时认为超简单,就一个链接地址+参数嘛,于是,立即开工,随意写了个网址,后面一拼参数,生成二维码,搞定!就等着前端给我小程序的地址了,然后就可以了。但是小程序的网址是隐藏的,获取不到,emmmm,行吧,谁让咱菜呢,这都没想到,接着找方法吧,祭出法宝-百度,百度一番,找到了https://blog.csdn.net/weixin_39927850/article/details/81038024,这是原文地址。微信小程序官方开放了3个创建二维码的接口,其中有一个是生成二维码的,还有一个是葵花状的小程序码,我这里就用php生成二维码。只需两部:1.获取Access_token 微信开发文档有请求接口: 要把自己的小程序的APPID和APPSECRET获取到
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
2. 然后自己拼接一下小程序的路径:path"=>"pages/index/index?id=123","width"=> 150
完整代码:
<?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文件,然后引入就可以了。
然后我将这段代码做了点小改动 更贴合自己的代码逻辑,我将这段代码写到了function.php文件中
function qrcod($pid,$code){ header(‘content-type:text/html;charset=utf-8‘); //配置APPID、APPSECRET $APPID = "wx6cafbcf25cde43af"; $APPSECRET = "6a66232d8e8aa7707560c88357602755"; //获取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"; if (!empty($pid)) { //个人 $param = json_encode(array("path"=>"pages/tabbar/Home/Home?pid=$pid","width"=> 300)); $img = "qrcodes/" . $pid . ‘.jpg‘; //就这个uid }else{ //网吧 $param = json_encode(array("path"=>"pages/tabbar/Home/Home?code=$code","width"=> 300)); $img = "qrcodes/" . $code . ‘.jpg‘; //就这个uid } //POST参数 $result = httpRequest( $qcode, $param,"POST"); //生成二维码 $url = ROOT_PATH . ‘/‘ . $img; file_put_contents($url, $result); $a=array(); $a[‘qrcode‘] = "https://peiwan.cyk.ink" . ‘/‘ . $img; $base64_image ="data:image/jpeg;base64,".base64_encode( $result ); return $a; }
//把请求发送到微信服务器换取二维码 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; }
这样,我不管在什么地方使用这个代码,只需要调用qrcod方法就可以了,