AliYunVodService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Log;
  4. use ShaoZeMing\AliVod\SDK\GetPlayInfoRequest;
  5. use ShaoZeMing\Aliyun\Core\DefaultAcsClient;
  6. use ShaoZeMing\Aliyun\Core\Profile\DefaultProfile;
  7. class AliYunVodService
  8. {
  9. public function getPlayUrlByVideoId($videoId)
  10. {
  11. try {
  12. $client = $this->initVodClient(config('aliyunvod.vod.AccessKeyID'), config('aliyunvod.vod.AccessKeySecret'));
  13. $playInfo = $this->getPlayInfo($client, $videoId);
  14. if($playInfo && is_object($playInfo)){
  15. return $playInfo->PlayInfoList->PlayInfo[0]->PlayURL;
  16. }else{
  17. return '';
  18. }
  19. } catch (\Exception $e) {
  20. Log::debug('获取视频地址失败:' . $e->getMessage());
  21. return '';
  22. }
  23. }
  24. private function initVodClient($accessKeyId, $accessKeySecret)
  25. {
  26. $regionId = 'cn-shanghai'; // 点播服务接入区域
  27. $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
  28. return new DefaultAcsClient($profile);
  29. }
  30. private function getPlayInfo($client, $videoId)
  31. {
  32. $request = new GetPlayInfoRequest();
  33. $request->setVideoId($videoId);
  34. $request->setAcceptFormat('JSON');
  35. $playConfig = ['PlayDomain' => 'http://oss.caihongxingqiu.net/'];
  36. $request->setPlayConfig(json_encode($playConfig));
  37. return $client->getAcsResponse($request);
  38. }
  39. }