您的位置:首页 > 移动开发 > 微信开发

PHP生成微信小程序二维码

2018-02-10 10:46 651 查看
通过后台接口可以获取小程序任意页面的二维码,扫描该二维码可以直接进入小程序对应的页面。官方推荐生成并使用小程序码,它具有更好的辨识度。目前有3个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口。

接口A: 适用于需要的码数量较少的业务场景 接口地址:
https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
参数如下:



注意:通过该接口生成的小程序码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。

接口B:适用于需要的码数量极多,或仅临时使用的业务场景:
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
参数如下:



注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
// 这是首页的 js
Page({
onLoad: function(options) {
// options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
var scene = decodeURIComponent(options.scene)
}
})

接口C:适用于需要的码数量较少的业务场景:
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
参数如下:



注意:通过该接口生成的小程序二维码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。

这几个接口POST 参数需要转成 json 字符串,不支持 form 表单提交。接口A加上接口C,总共生成的码数量限制为100,000,请谨慎调用。
这里以接口B为例,讲一下生成二维码图片并保存本地服务器:
1. 获取 access_token
access_token 是全局唯一接口调用凭据,开发者调用各接口时都需使用 access_token,需妥善保存。做过微信开发和公众号开发,这都是最基本的,这里获取方法跟公众号获取 access_token 一模一样,方法见: 获取微信基础接口凭证Access_token
2.生成二维码/**
* 生成小程序二维码
* @param string $qr_path 存储路径,相对于程序根目录(例如:/Public/Qrcode/)
* @param string $filename 存储的图片名称(例如:aaa.png)
* @param string $scene 二维码场景值
* @param string $page 二维码跳转页面
* @param string $expires_in 二维码有效时间
* @return [type] [description]
*/
function create_qrcode($qr_path,$filename,$scene,$page='',$expires_in=7200){

if(empty($qr_path)) return array('status'=>0,'info'=>'缺少存储路径');
if(empty($filename)) return array('status'=>0,'info'=>'请确定存储的图片名称');
if(empty($scene)) return array('status'=>0,'info'=>'缺少二维码场景值');

if(!is_dir('.'.$qr_path)){ // ./Public/Qrcode/
mkdir(iconv("GBK","UTF-8",'.'.$qr_path),0777,true);
}
$file = $qr_path.$filename; // /Public/Qrcode/aaa.png
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$fileUrl = $protocol.$_SERVER['HTTP_HOST'].$file; // http://yourhost/Public/Qrcode/aaa.png $savePath = '.'.$file; // ./Public/Qrcode/aaa.png
if(file_exists($savePath)){
//当前时间-文件创建时间<过期时间
if( (time()-filemtime($savePath)) < $expires_in ) return array('status'=>1,'info'=>$fileUrl);
}

$accessToken = 'xxxxxxxxxxxxxxxxxxxxxx'; // 获取到的 access_token
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$accessToken;
$qrcode = array(
'scene' => $scene,
'width' => 200,
'page' => $page,
'auto_color' => true
);
$result = request($url,true,'POST',json_encode($qrcode));
$errcode = json_decode($result,true)['errcode'];
$errmsg = json_decode($result,true)['errmsg'];
if($errcode) return array('status'=>0,'info'=>$errmsg);
$res = file_put_contents($savePath,$result); // 将获取到的二维码图片流保存成图片文件

if($res===false) return array('status'=>0,'info'=>'生成二维码失败');
return array('status'=>1,'info'=>$fileUrl); //返回本地图片地址
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: