...
PHP

php 动态小程序码生成类

中间遇到很多坑  不说了  直接上代码

Miniqrcode类

Miniqrcode.php
<?php
/**
* 生成小程序码
*/
class Miniqrcode{

private $grant_type = 'client_credential';
private $appid = '';
private $secret = '';

public function __construct($appid,$secret) {
$this->appid = $appid;
$this->secret = $secret;
}

public function getQrcode($data)
{
// 获取at
$at = $this->getAccessToken();
if($at === false){
return false;
}
$data['access_token'] = $at;
// 获取小程序码
$qrcode = $this->getUnlimited($data);
return $qrcode;
}

/**
* 获取小程序AccessToken
*/
private function getAccessToken()
{
// GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
$res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type={$this->grant_type}&appid={$this->appid}&secret={$this->secret}");
$res = json_decode($res,1);
if(array_key_exists('errcode',$res) && $res['errcode'] != 0){
return false;
}
return $res['access_token'];
}

/**
* $data
* access_token string 是 接口调用凭证
* scene string 是 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
* page string 主页 否 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
* width number 430 否 二维码的宽度,单位 px,最小 280px,最大 1280px
* auto_color boolean false 否 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
* line_color Object {"r":0,"g":0,"b":0} 否 auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
* is_hyaline boolean false 否 是否需要透明底色,为 true 时,生成透明底色的小程序
*/
private function getUnlimited($data)
{
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$data['access_token']}";
unset($data['access_token']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
// data数据必须是json 官方没写 真坑
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );
$output = curl_exec($ch);
curl_close($ch);
if(strpos($output,'errcode')!==false){
return false;
}
// 输出base64数据
return 'data:image/jpg;base64,'.base64_encode($output);
}

}

使用方法

<?php
include './Minqrcode.php';
$MQ = new Miniqrcode('wx....','xxxxx');
$res = $MQ->getQrcode([
"scene"=>'wsas'
]);
print_r($res);


js localStorage 创建、清除、获取 Vue阻止上级冒泡的方法 子原元素事件触发父元素事件 禁止父元素事件冒泡
biu biu biu
分享一个免费电路(arduino、单片机)仿真软件SimulIDE,遥遥领先! arduino esp8266 websocket 简单示例 RustDesk免费的PC/android安卓手机远程桌面开源软件 小米手环解锁MacOS苹果笔记本MacBook Pro electron 防止多开 只允许一个实例允许并在重复打开后显示第一个实例