微信小程序三种生成二维码的接口如下:
接口A: 适用于需要的码数量较少的业务场景 接口地址:
1)、永久有效
2)、数量有限制
3)、支持二维码个性化
https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
接口B:适用于需要的码数量极多的业务场景
1)、永久有效
2)、数量无限
3)、scene是自定义的参数
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
接口C:适用于需要的码数量较少的业务场景
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
1)、永久有效
2)、数量有限制
共同性质:都需要获取到微信的[access_token]
Redis类简单封装了下:如下(放在application/server目录)
namespace app\server; class Redis{ protected static $redis; public static function get_instence() { if(!self::$redis){ if (!extension_loaded('redis')) { throw new \BadFunctionCallException('not support: redis'); } $redis = new \Redis(); $config = \think\Config::get('redis'); $redis->connect($config['host'], $config['port']); self::$redis = $redis; } return self::$redis; } }
获取access_token的方法如下:
public function get_access(){ $appid = Config::get('wechat.appid'); //微信appid $secret = Config::get('wechat.secret'); //微信secret $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; //我将access_token 保存在redis中 $redis_access = \app\server\Redis::get_instence()->get('access_token'); if(empty($redis_access)){ $res = $this->httpRequest($url); \app\server\Redis::get_instence()->set('access_token',$res,7200); return $res; } return $redis_access; }
获取小程序二维码的方法
/** *$params 自定义参数 *$access_token get_access获取到的token */ public function get_code($params ,$access_token) { $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}"; $data = json_encode([ "width" => 150, "scene" => $params ]); $res = $this->httpRequest($url,$data,'POST'); $base64_image ="data:image/jpeg;base64,".base64_encode( $res ); return $base64_image; }
httpRequest curl 方法请求封装
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; }
base64图片本地化方法封装
public function file_put($base64_image_content,$new_file) { header('Content-type:text/html;charset=utf-8'); if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){ return true; }else{ return false; } } }
生成二维码并且本地化(实际调用)
//获取access_token $access_token = $this->get_access(); //返回的是json $access_token = json_decode($access_token,true); //调用方法获取二维码(返回的是base64格式的图片) $res = $this->get_code('params',$access_token['access_token']); //二维码本地化路径 $path = 'uploads/'.uniqid().'.jpg'; //调用本地化方法 $this->file_put($res,ROOT_PATH.$path);
这样就完整的生成了小程序二维码。
总结下有可能出现的坑
1、获取二维码是post请求,注意别错了
2、返回的是base64格式的图片
3、如果需要本地化,注意 file_put 方法替换base64头空格等,不然会导致file_put_content调用失败。