记录一下服务端处理
注意一下坑点,使用官方SDK文件时,记得把以下文件的printLog注释掉。不注释虽然能上传成功,但一直报服务器错误
\vendor\voduploadsdk\uploader\AliyunVodUtils.php
\voduploadsdk\uploader\AliyunVodUploader.php
这两个文件的printLog注释掉,最好全SDK搜索一篇
<?php
use vod\Request\V20170321\GetPlayInfoRequest;
$accessKeyId = '';
$accessKeySecret = '';
require_once WEB_ROOT.'vendor/voduploadsdk/Autoloader.php';// voduploadsdk为官方sdk目录文件
try{
$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret, 'eu-central-1');// 第三个参数是选择地区
$uploadVideoRequest = new UploadVideoRequest(WEB_ROOT . ltrim($fileSavePath, '/'), '上传显示文件的名称定义');// fileSavePath为客户端文件路径
$uploadVideoRequest->setStorageLocation('outin-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.oss-eu-central-1.aliyuncs.com');// 设置存储地址
$userData = array(
"MessageCallback"=>array("CallbackURL"=>""),
"Extend"=>array("localId"=>"", "test"=>"")
);
$uploadVideoRequest->setUserData(json_encode($userData));
// 获取video_id值
$video_id = $uploader->uploadLocalVideo($uploadVideoRequest);
//初始化视频客户端
$profile = DefaultProfile::getProfile('eu-central-1', $accessKeyId, $accessKeySecret);
$client = new DefaultAcsClient($profile);
$request = new GetPlayInfoRequest();
$request->setVideoId($video_id);
$request->setAcceptFormat('JSON');
$videoInfo = $client->getAcsResponse($request);
$videoInfo1 = json_decode(json_encode($videoInfo),true);
// 获取播放地址
$video_url = $videoInfo1['PlayInfoList']['PlayInfo'][0]['PlayURL'] ?? '';
// 个人需要用到的数据
$data = json_encode(['video_id' => $video_id, 'video_url' => $video_url]);
}catch (\Exception $e){
throw new \Exception($e->getMessage());
}
另一种方法
layui前端上传 + 后端处理
<div style="margin-left: 30px">
<button type="button" class="layui-btn" id="videoUploadBtn"><i class="layui-icon"></i>上传视频</button>
<input type="text" id="video" value="">
</div>
<link href="/public/js/layui/css/layui.css" type="text/css" rel="stylesheet" />
<script src="/public/js/layui/layui.all.js"></script>
<script>
layui.use('upload', function () {
var $ = layui.jquery
, upload = layui.upload;
upload.render({
elem: '#videoUploadBtn'
, url: '{:url(\'admin/asset/videoUpload\')}' //改成你自己的上传接口
, accept: 'video' //视频
, done: function (res) {
console.log(res);
// if (res == '失败') {
// layer.msg('上传失败');
// return;
// }
// $('#video').val(res);
// layer.msg('上传成功');
}
});
});
</script>
public function videoUpload(){
// 上传文件
$request = Request::instance();
$file = $request->file('file');//获取文件
$info = $file->move('./upload/asset');
$getSaveName = str_replace("\\", "/", $info->getSaveName());
$fileSavePath = $_SERVER['DOCUMENT_ROOT'] . '/upload/asset/'. $getSaveName;
$filesystems = Config('filesystems');
$vodConfig = $filesystems['disks']['aliyun_vod'] ?? [];
require_once WEB_ROOT.'vendor/voduploadsdk/Autoloader.php';
try{
$uploader = new AliyunVodUploader($vodConfig['accessKeyId'], $vodConfig['accessKeySecret'], 'eu-central-1');
$uploadVideoRequest = new UploadVideoRequest($fileSavePath, $getSaveName);
$uploadVideoRequest->setStorageLocation('outin-c39706225f1111ec9b9800163e00865b.oss-eu-central-1.aliyuncs.com');
$userData = array(
"MessageCallback"=>array("CallbackURL"=>""),
"Extend"=>array("localId"=>"", "test"=>"")
);
$uploadVideoRequest->setUserData(json_encode($userData));
$video_id = $uploader->uploadLocalVideo($uploadVideoRequest);
//初始化视频客户端
$profile = DefaultProfile::getProfile('eu-central-1', $vodConfig['accessKeyId'], $vodConfig['accessKeySecret']);
$client = new DefaultAcsClient($profile);
$request = new GetPlayInfoRequest();
$request->setVideoId($video_id);
$request->setAcceptFormat('JSON');
$videoInfo = $client->getAcsResponse($request);
$videoInfo1 = json_decode(json_encode($videoInfo),true);
// error_log(print_r($videoInfo1, true)."\n", 3, 'data.log');
// error_log(print_r($videoInfo1['PlayInfoList']['PlayInfo'][0]['PlayURL'], true)."\n", 3, 'data.log');
$video_url = $videoInfo1['PlayInfoList']['PlayInfo'][0]['PlayURL'] ?? '';
echo $video_url;exit();
}catch (\Exception $e){
return '';
}
}