VodService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use think\Request;
  13. use service\JsonService;
  14. class VodService
  15. {
  16. protected static $method='GET'; //获取方式
  17. protected static $AccessKeyId=''; //阿里云AccessKeyId
  18. protected static $accessKeySecret=''; //阿里云AccessKeySecret
  19. final static function init()
  20. {
  21. self::$AccessKeyId=SystemConfigService::get('accessKeyId');//阿里云AccessKeyId
  22. self::$accessKeySecret=SystemConfigService::get('accessKeySecret');//阿里云AccessKeySecret
  23. }
  24. /**获取视频上传地址和凭证
  25. * @param string $videoId
  26. * @param string $FileName
  27. * @param string $type 1=获取视频上传凭证 2=获取视频播放凭证 3=获取视频播放地址 4=删除完整视频
  28. */
  29. public static function videoUploadAddressVoucher($FileName='',$type=1,$videoId='',$image='')
  30. {
  31. self::init();
  32. $apiParams=[];
  33. $site_url=SystemConfigService::get('site_url');
  34. $https = "/^https:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/";
  35. if(preg_match($https,$site_url)){
  36. $requestUrl="https://vod.cn-shanghai.aliyuncs.com/?";
  37. }else{
  38. $requestUrl="http://vod.cn-shanghai.aliyuncs.com/?";
  39. }
  40. if($videoId!='' && $type==1){
  41. $apiParams['Action'] ='RefreshUploadVideo';
  42. $apiParams['VideoId'] = $videoId;
  43. }else if($videoId!='' && $type==2){
  44. $apiParams['Action'] ='GetVideoPlayAuth';
  45. $apiParams['VideoId'] = $videoId;
  46. }else if($videoId!='' && $type==3){
  47. $apiParams['Action'] ='GetPlayInfo';
  48. $apiParams['VideoId'] = $videoId;
  49. }else if($videoId!='' && $type==4){
  50. $apiParams['Action'] ='DeleteVideo';
  51. $apiParams['VideoIds'] = $videoId;
  52. }else if($videoId=='' && $type==1){
  53. $apiParams['Action'] ='CreateUploadVideo';
  54. $apiParams['Title'] = self::video_name($FileName);
  55. $apiParams['FileName'] = $FileName;
  56. $apiParams['CoverURL'] = $image;
  57. }
  58. $apiParams['AccessKeyId'] = self::$AccessKeyId;
  59. $apiParams['Format'] = 'JSON';
  60. $apiParams['SignatureMethod'] = 'HMAC-SHA1';
  61. $apiParams['SignatureVersion'] = '1.0';
  62. $apiParams['SignatureNonce'] = md5(uniqid(mt_rand(), true));
  63. $apiParams['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  64. $apiParams['Version'] = '2017-03-21';
  65. $apiParams['Signature']=self::computeSignature($apiParams,self::$accessKeySecret);
  66. foreach ($apiParams as $apiParamKey => $apiParamValue) {
  67. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
  68. }
  69. return substr($requestUrl, 0, -1);
  70. }
  71. public static function signString($source, $accessSecret)
  72. {
  73. return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
  74. }
  75. /**
  76. * 视频名称
  77. */
  78. public static function video_name($FileName)
  79. {
  80. return mb_substr(substr($FileName, strrpos($FileName, '.')+1),0,128,'utf8');
  81. }
  82. /**签名机制
  83. * @param $parameters
  84. * @param $accessKeySecret
  85. * @return mixed
  86. */
  87. public static function computeSignature($parameters, $accessKeySecret)
  88. {
  89. ksort($parameters);
  90. $canonicalizedQueryString = '';
  91. foreach ($parameters as $key => $value) {
  92. $canonicalizedQueryString .= '&' . self::percentEncode($key) . '=' . self::percentEncode($value);
  93. }
  94. $stringToBeSigned =
  95. self::$method.'&%2F&' . self::percentEncode(substr($canonicalizedQueryString, 1));
  96. return self::signString($stringToBeSigned, $accessKeySecret . '&');
  97. }
  98. /**
  99. * @param $str
  100. * @return string|string[]|null
  101. */
  102. public static function percentEncode($str)
  103. {
  104. $res = urlencode($str);
  105. $res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
  106. $res = preg_replace('/%7E/', '~', $res);
  107. return $res;
  108. }
  109. }