tp6 微信分享码 存储到服务器(或者存储到oss)
文档 不会
微信文档地址
请求的参数
这个非常重要我就传一个id 不需要这个就能获取到 传递多个参数的话就需要encodeURIComponent
开始调用接口—
public function getPromotionCode($uid)
{
$data = [
"page"=>"pages/index/index",
"scene"=> $uid,
'env_version' => 'develop', //正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
'width'=> 600,
];
$datass = json_encode($data);
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$this->getAccessToken(); //这里获取的token
Log::info("URL ". $url);
$res = curl_post($url , $datass);
if (isset(json_decode($res , true)['errcode'])) {
Log::info("code res " . $res);
abortExecute("token获取失败");
}
# 存储再服务器上的路径 getcwd 获取到public下的绝对路径
$path = getcwd().'/Qrcode';
if(!file_exists($path)){ //判断目录是否存在
mkdir($path);
}
$filename = md5(time());
$path = $path.'/'.$filename.'.png'; //最后要写入的目录及文件名
// 创建将数据流文件写入我们创建的文件内容中
$ifp = fopen( $path, "w" );
fwrite( $ifp, $res );
fclose( $ifp );
# 到这里 图片就存储到服务器上了
# 如果需要上传到oss的话
// 执行上传并获取返回 oss 信息
#参数 $localpsth 图片再服务器上的路径 这里传$path 就可以
#参数 $object oss上的路径 下边是我拼接的我的路径
$scene = 'common';
# 验证类型
$object = 'images/'.($scene?($scene.'/'):'').date('Ymd').'/'.Date::getFlashTime().'_'.mt_rand(100000,999999).'.jpg'; //oss上的路径
$res = (new Oss())->localUpload($localPath, $object);
# 记得删除本地的图片
unlink($path); //删除本地的图片
}
public function localUpload($path , $object)
{
try{
# uploadfile 这个方法是oss的方法 应该oss扩展拉下来之后就能看到
$res = $this->client->uploadFile($this->bucketName ,$object , $path );
$resInfo = [];
# 返回文件地址 大小 MD5 类型
if(empty($res['oss-requestheaders']) || empty($res['oss-request-url'])){
abortExecute(API_ERROR, '文件上传返回数据错误');
}
$resInfo = $res['oss-requestheaders'];
$resInfo['url'] = $res['oss-request-url'];
return $resInfo;
} catch(OssException $e) {
abortExecute(API_ERROR, __FUNCTION__ . ": FAILED;".$e->getMessage());
} catch(Exception $e) {
abortExecute(API_ERROR, __FUNCTION__ . ": FAILED;".$e->getMessage());
}
}
function curl_post($url , $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$output = curl_exec($ch);
return $output;
}
/**
* Uploads a local file
*
* @param string $bucket bucket name
* @param string $object object name
* @param string $file local file path
* @param array $options
* @return null
* @throws OssException
*/
public function uploadFile($bucket, $object, $file, $options = NULL)
{
$this->precheckCommon($bucket, $object, $options);
OssUtil::throwOssExceptionWithMessageIfEmpty($file, "file path is invalid");
$file = OssUtil::encodePath($file);
if (!file_exists($file)) {
throw new OssException($file . " file does not exist");
}
$options[self::OSS_FILE_UPLOAD] = $file;
$file_size = sprintf('%u',filesize($options[self::OSS_FILE_UPLOAD]));
$is_check_md5 = $this->isCheckMD5($options);
if ($is_check_md5) {
$content_md5 = base64_encode(md5_file($options[self::OSS_FILE_UPLOAD], true));
$options[self::OSS_CONTENT_MD5] = $content_md5;
}
if (!isset($options[self::OSS_CONTENT_TYPE])) {
$options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $file);
}
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_OBJECT] = $object;
$options[self::OSS_CONTENT_LENGTH] = $file_size;
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}