当前位置:网站首页>php获取微信小程序码并存储到oss
php获取微信小程序码并存储到oss
2022-08-11 10:35:00 【蓝枫秋千】
直接上代码
获取小程序码(
这里返回的是文件流
)// 获取access_token $access_token = $this->getAccessToken(); // 需要跳转的页面和页面参数,小程序码的宽度 $params = [ "path" => "pages/taskDetail/taskDetail?id=$taskId", "width" => 430 ]; // 这里的参数需要使用raw的方式传参(!!!!这里需要注意) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/wxa/getwxacode?access_token=$access_token" ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($params));// 必须为字符串 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));// 必须声明请求头 // 如果没有错误的话返回的就是小程序码的文件流 $result = curl_exec($ch); if(isset($result['errcode'])){ //查看文档错误代码 return setResponse(config('status.error'), "获取小程序码出错", $result); }
将文件流转化为文件并存储到服务器上
//没有出现错误代码 会返回二进制文件流,可以转成base64直接使用也可以转成file
//以下是将二进制流转成file并写入本地
$data = date('Ymd');
// 服务器文件存储路径
$path = $basePath . $data;
if(!file_exists($path)){
//判断目录是否存在
mkdir($path);
}
$filename = md5($taskId);
$filePath = $path.'/'.$filename.'.png'; //最后要写入的目录及文件名
// 创建将数据流文件写入我们创建的文件内容中
$ifp = fopen( $filePath, "w" );
fwrite( $ifp, $result );
fclose( $ifp );
- 将服务器的文件存储到oss服务器上
// oss配置信息
$accessKeyId = config("aliyun.accessKeyId");
$accessKeySecret = config("aliyun.accessKeySecret");
$endpoint = config("aliyun.endpoint");
$bucket = 'zhushang-applet-code';
$ossPath = "$taskId.png";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// 如果有自定义的文件名,则使用自定义的文件名
$aa = $ossClient->uploadFile($bucket, $ossPath, $filePath);
// 这里的url既是oss文件地址
$url = $aa['info']['url'];
} catch (OssException $e) {
$url = "获取小程序码出错";
return setResponse(config('status.error'), '存储小程序码失败', $e->getMessage());
}
边栏推荐
猜你喜欢
随机推荐
MySQL数据库基础_常用数据类型_表设计
【luogu CF1286E】Fedya the Potter Strikes Back(字符串)(KMP)(势能分析)(线段树)
mysql常用的一些时间函数记录
Simple interaction between server and client
困扰所有SAP顾问多年的问题终于解决了
一站式PCBA组装加工有哪些环节?
> 家乡旅游景点网页作业制作 网页代码运用了DIV盒子的使用方法,如盒子的嵌套、浮动、margin、border、backgro
SAP 产品增强技术回顾
chrome设置为深色模式(包括整个网页)
go基础之并发
突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
困扰所有SAP顾问多年的问题终于解决了
Primavera Unifier - AEM Form Designer Essentials
LeetCode · Question of the Day · 1417. Reformatting String · Simulation
计算数组某个元素的和
字符函数和字符串函数的进阶
logstash/filebeat only receives data from the most recent period
Huawei WLAN Technology: AC/AP Experiment
【Prometheus】Alertmanager告警全方位讲解
Revelations!The former Huawei microservice expert wrote 500 pages of practical notes on the landing architecture, which has been open sourced