VideoDemand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\SDK\Vod\V20170321\Vod;
  7. use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest;
  8. use AlibabaCloud\Tea\Exception\TeaError;
  9. use Darabonba\OpenApi\Models\Config;
  10. use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
  11. use AlibabaCloud\SDK\Vod\V20170321\Models\RefreshUploadVideoRequest;
  12. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  13. use AlibabaCloud\Tea\Utils\Utils;
  14. use \Exception;
  15. use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
  16. require_once '../vendor/aliyunmail/aliyun-php-sdk-core/Config.php'; // 假定您的源码文件和aliyun-php-sdk处于同一目录
  17. $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
  18. if (file_exists($path)) require_once $path;
  19. date_default_timezone_set('PRC');
  20. /**
  21. * @title 阿里云视频点播【接口数据及格式为视频点播接口返回】
  22. * @controller VideoDemand
  23. * @group base
  24. */
  25. class VideoDemand extends Base
  26. {
  27. protected $account_name = 'vod@1122764885952286.onaliyun.com'; // 账号
  28. protected $login_secret = 'NKna#5y1mKBp58k)qqwJHhRcrUE!{ECT';// 密码
  29. protected $account_uid = '285664060640337784';// uid
  30. protected $access_key = 'LTAI5tN5SwDsN2km6PgZPScp';// key
  31. protected $access_secret = 'SqRcmQNYG8d7l2vjDLpVvibwT59bSj';//secret
  32. protected $role_arn= 'acs:ram::1122764885952286:role/voderole';//role_arn
  33. protected $role_session_name= 'voderole';//role_session_name
  34. /**
  35. * 使用AK&SK初始化账号Client
  36. * @param string $accessKeyId
  37. * @param string $accessKeySecret
  38. * @return Vod Client
  39. */
  40. public static function createClient($accessKeyId, $accessKeySecret){
  41. $config = new Config([
  42. // 您的 AccessKey ID
  43. "accessKeyId" => $accessKeyId,
  44. // 您的 AccessKey Secret
  45. "accessKeySecret" => $accessKeySecret
  46. ]);
  47. // 访问的域名
  48. $config->endpoint = "vod.cn-shanghai.aliyuncs.com";
  49. return new Vod($config);
  50. }
  51. /**
  52. * 获取本地文件对象
  53. * @return \think\File
  54. */
  55. private function getUploadFile()
  56. {
  57. try {
  58. return $this->request->file('file');
  59. } catch (\Exception $e) {
  60. $this->error(lang($e->getMessage()));
  61. }
  62. }
  63. /**
  64. * @title 获取视频播放凭证[STS方式]
  65. * @desc 获取视频播放凭证[STS方式]
  66. * @author qc
  67. * @url /api/Video_demand/getVideoCredentials
  68. * @method GET
  69. * @tag 视频播放凭证
  70. * @header name:Authorization require:1 desc:Token
  71. * @return name:RequestId type:string default:-- desc:RequestId
  72. * @return name:Arn type:string default:-- desc:RoleArn(Arn)
  73. * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
  74. * @return name:SecurityToken type:string default:-- desc:SecurityToken
  75. * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
  76. * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
  77. * @return name:Expiration type:string default:-- desc:Expiration
  78. */
  79. public function getVideoCredentials()
  80. {
  81. //构建一个阿里云客户端,用于发起请求。
  82. //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
  83. AlibabaCloud::accessKeyClient($this->access_key, $this->access_secret)->regionId('cn-hangzhou')->asDefaultClient();
  84. //设置参数,发起请求。
  85. try {
  86. $result = AlibabaCloud::rpc()
  87. ->product('Sts')
  88. ->scheme('https') // https | http
  89. ->version('2015-04-01')
  90. ->action('AssumeRole')
  91. ->method('POST')
  92. ->host('sts.aliyuncs.com')
  93. ->options([
  94. 'query' => [
  95. 'RegionId' => "cn-hangzhou",
  96. 'RoleArn' => $this->role_arn,
  97. 'RoleSessionName' =>$this->role_session_name,
  98. ],])
  99. ->request();
  100. $ret = $result->toArray();
  101. $ret['Credentials']['region'] = 'cn-shanghai';
  102. $this->success('请求成功',$ret);
  103. } catch (ClientException $e) {
  104. $this->error($e->getErrorMessage() . PHP_EOL);
  105. } catch (ServerException $e) {
  106. $this->error($e->getErrorMessage() . PHP_EOL);
  107. }
  108. }
  109. /**
  110. * @title 获取视频播放凭证[通过视频ID获取]
  111. * @desc 获取视频播放凭证[通过视频ID获取]
  112. * @author qc
  113. * @url /api/Video_demand/getVideoCredentials
  114. * @method GET
  115. * @tag 视频播放凭证
  116. * @header name:Authorization require:1 desc:Token
  117. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  118. * @return name:playAuth type:string default:-- desc:playAuth(时效3000s)
  119. * @return name:requestId type:string default:-- desc:requestId
  120. */
  121. public function getVideoCredentialsByVideoId()
  122. {
  123. $client = self::createClient($this->access_key, $this->access_secret);
  124. $getVideoPlayAuthRequest = new GetVideoPlayAuthRequest([
  125. 'videoId'=>input('ali_vid'),
  126. "authInfoTimeout" => 3000
  127. ]);
  128. $runtime = new RuntimeOptions([]);
  129. try {
  130. $res = $client->getVideoPlayAuthWithOptions($getVideoPlayAuthRequest, $runtime);
  131. $json_res = json_encode($res,true);
  132. $res_arr = json_decode($json_res,true);
  133. }catch (\Exception $error){
  134. if (!($error instanceof TeaError)) {
  135. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  136. }
  137. $this->error($error->message);
  138. }
  139. // 复制代码运行请自行打印 API 的返回值
  140. $this->success('ok',$res_arr['body'] );
  141. }
  142. /**
  143. * @title 获取视频上传凭证
  144. * @desc 获取视频上传凭证
  145. * @author qc
  146. * @url /api/Video_demand/createUploadVideo
  147. * @method POST
  148. * @tag 获取视频上传凭证
  149. * @header name:Authorization require:1 desc:Token
  150. * @name name:file type:string default:-- desc:视频文件
  151. * @return name:requestId type:string default:-- desc:requestId
  152. * @return name:uploadAddress type:string default:-- desc:uploadAddress
  153. * @return name:uploadAuth type:string default:-- desc:uploadAuth
  154. * @return name:videoId type:string default:-- desc:videoId
  155. */
  156. public function createUploadVideo(){
  157. if (!($file = $this->getUploadFile()) || empty($file)) $this->error('文件上传异常,文件可能过大或未上传!');
  158. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) $this->error('文件上传类型受限,请在后台配置!');
  159. if ($file->checkExt('php,sh')) $this->error('可执行文件禁止上传到本地服务器!');
  160. $size = $file->getSize();
  161. $file_name = $file->getInfo('name');
  162. $extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  163. $up_param = [
  164. 'fileName'=>$file_name,
  165. 'FileSize'=>$size,
  166. 'title'=>'视频上传',
  167. ];
  168. try {
  169. $client = self::createClient($this->access_key, $this->access_secret);
  170. $createUploadVideoRequest = new CreateUploadVideoRequest($up_param);
  171. $runtime = new RuntimeOptions([]);
  172. $res = $client->createUploadVideoWithOptions($createUploadVideoRequest, $runtime);
  173. $json_res = json_encode($res,true);
  174. $res_arr = json_decode($json_res,true);
  175. }catch (\Exception $error){
  176. if (!($error instanceof TeaError)) {
  177. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  178. }
  179. $this->error($error->message);
  180. }
  181. $this->success('ok',$res_arr['body']);
  182. }
  183. /**
  184. * @title 刷新视频上传凭证
  185. * @desc 刷新视频上传凭证
  186. * @author qc
  187. * @url /api/Video_demand/refreshUploadVideoToken
  188. * @method GET
  189. * @tag 刷新视频上传凭证
  190. * @header name:Authorization require:1 desc:Token
  191. * @header name:Authorization require:1 desc:Token
  192. * @param name:ali_vid type:string default:-- desc:videoId
  193. * @return name:uploadAddress type:string default:-- desc:uploadAddress
  194. * @return name:uploadAuth type:string default:-- desc:uploadAuth
  195. * @return name:videoId type:string default:-- desc:videoId
  196. */
  197. public function refreshUploadVideoToken()
  198. {
  199. $client = self::createClient($this->access_key, $this->access_secret);
  200. $refreshUploadVideoRequest = new RefreshUploadVideoRequest([
  201. 'videoId'=>input('ali_vid'),
  202. ]);
  203. $runtime = new RuntimeOptions([]);
  204. try {
  205. // 复制代码运行请自行打印 API 的返回值
  206. $res = $client->refreshUploadVideoWithOptions($refreshUploadVideoRequest, $runtime);
  207. $json_res = json_encode($res,true);
  208. $res_arr = json_decode($json_res,true);
  209. }
  210. catch (\Exception $error) {
  211. if (!($error instanceof TeaError)) {
  212. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  213. }
  214. $this->error($error->message);
  215. }
  216. $this->success('ok',$res_arr['body'] );
  217. }
  218. /**
  219. * @title 获取视频播放地址[通过视频ID获取]
  220. * @desc 获取视频播放地址[通过视频ID获取]
  221. * @author qc
  222. * @url /api/Video_demand/getVideoPath
  223. * @method GET
  224. * @tag 视频播放凭证
  225. * @header name:Authorization require:1 desc:Token
  226. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  227. * @return name:playInfoList type:array default:-- desc:playInfoList
  228. * @return name:playInfoList.playInfo.playURL type:string default:-- desc:播放地址
  229. */
  230. public function getVideoPath()
  231. {
  232. $client = self::createClient($this->access_key, $this->access_secret);
  233. $getVideoPlayAuthRequest = new GetPlayInfoRequest(['videoId'=>input('ali_vid')]);
  234. $runtime = new RuntimeOptions([]);
  235. try {
  236. $res = $client->getPlayInfoWithOptions($getVideoPlayAuthRequest, $runtime);
  237. $json_res = json_encode($res,true);
  238. $res_arr = json_decode($json_res,true);
  239. }catch (\Exception $error){
  240. if (!($error instanceof TeaError)) {
  241. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  242. }
  243. $this->error($error->message);
  244. }
  245. $this->success('ok',$res_arr['body'] );
  246. }
  247. }