直接上代码
-
获取小程序码(这里返回的是文件流 )
$access_token = $this->getAccessToken();
$params = [
"path" => "pages/taskDetail/taskDetail?id=$taskId",
"width" => 430
];
$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);
}
-
将文件流转化为文件并存储到服务器上
$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服务器上
$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 = $aa['info']['url'];
} catch (OssException $e) {
$url = "获取小程序码出错";
return setResponse(config('status.error'), '存储小程序码失败', $e->getMessage());
}
|