阿里云视频点播
首先我们找到阿里云的PHP sdk 下载下来 我这里用的是2017-03-21版本的 php框架是Yii 前端Vue
下载下来之后将整个文件解压放到 vendor目录下 我在这里给文件夹改了个名 改成了aliyun-vod-upload-sdk
在阿里云里需要新建一个用户 获取他的accessKeyId 和accessKeySecret,在这里记得给用户权限否则上传会报错
准备工作做完了,然后在Yii 控制器中
/** * 阿里云视频上传 * @Author WangSai * @DateTime 2020-06-18 * @version [version] * @return [type] [description] */ public function actionUploadVideo() { require_once Yii::getAlias('@vendor')."/aliyun-vod-upload-sdk/voduploadsdk/Autoloader.php"; //引入 autoloader 它会自动帮我们去引入我们需要的类 $accessKeyId = '****'; $accessKeySecret = '****'; $filePath = $_FILES['file']['tmp_name']; $name = explode('.',$_FILES['file']['name']); $count = count($name); $name = time().'.'.$name[$count-1]; $dir = 'uploads/vedio/'.$name; //注意要先保存到自己的服务器上 move_uploaded_file($filePath,$dir);$uploader = new \AliyunVodUploader($accessKeyId, $accessKeySecret); $uploadVideoRequest = new \UploadVideoRequest($dir, '视频上传'); $res = $uploader->uploadLocalVideo($uploadVideoRequest); //在这里我们只是上传了 阿里云视频点播并没有返回我们视频播放地址 需要调用它的接口 来查询视频播放地址 代码如getaddress中 官方参考 https://help.aliyun.com/document_detail/56124.html?spm=a2c4e.11153940.0.0.36417adf4vsYje $result = self::getAddress($res,$accessKeyId,$accessKeySecret); $res = json_decode($result,true); // $res['PlayInfoList']['PlayInfo'][0]['PlayURL']; 视频地址 return $this->msg(200,'',$res['PlayInfoList']['PlayInfo'][0]['PlayURL']); } /** * PHP 获取视频播放地址 */ public static function getAddress($VideoId,$keyId,$secr) { date_default_timezone_set("UTC");//阿里云服务时间为UTC $apiParams['Format'] = 'JSON'; $apiParams['VideoId'] = $VideoId; $apiParams['Version'] = '2017-03-21'; $apiParams['SignatureMethod'] = 'Hmac-SHA1'; $apiParams['SignatureNonce'] = substr(md5(rand(1, 99999999)), rand(1, 9), 14); $apiParams['SignatureVersion'] = '1.0'; $apiParams['Action'] = 'GetPlayInfo'; $apiParams['AccessKeyId'] = $keyId;//阿里云密钥ID $apiParams['Timestamp'] = date("Y-m-d\TH:i:s\Z"); $accessSecret = $secr;//阿里云密钥 $apiParams['Signature'] = self::computeSignature($credential = "GET",$apiParams, $accessSecret);//签名 $uri = http_build_query($apiParams); $url = 'http://vod.cn-shanghai.aliyuncs.com/?' . $uri; return self::getCurl($url); } /** * CURL */ public static function getCurl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); return $result; } /** * 阿里云签名机制(生成签名) */ public static function computeSignature($credential, $parameters, $accessKeySecret) { ksort($parameters); $stringToSign = strtoupper($credential) . '&' . self::percentEncode('/') . '&'; $tmp = ""; foreach ($parameters as $key => $val) { $tmp .= '&' . self::percentEncode($key) . '=' . self::percentEncode($val); } $tmp = trim($tmp, '&'); $stringToSign = $stringToSign . self::percentEncode($tmp); $key = $accessKeySecret . '&'; $hmac = hash_hmac("sha1", $stringToSign, $key, true); return base64_encode($hmac); } /** * url编码 * @param $str * @return mixed|string */ public static function percentEncode($value = NULL) { $en = urlencode($value); $en = str_replace("+", "%20", $en); $en = str_replace("*", "%2A", $en); $en = str_replace("%7E", "~", $en); return $en; }
评论 (0)