AliYunVodService.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. var_dump($playInfo);
  15. if($playInfo && is_object($playInfo)){
  16. return $playInfo->PlayInfoList->PlayInfo[0]->PlayURL;
  17. }else{
  18. return '';
  19. }
  20. } catch (\Exception $e) {
  21. Log::debug('获取视频地址失败:' . $e->getMessage());
  22. return '';
  23. }
  24. }
  25. private function initVodClient($accessKeyId, $accessKeySecret)
  26. {
  27. $regionId = 'cn-shanghai'; // 点播服务接入区域
  28. $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
  29. return new DefaultAcsClient($profile);
  30. }
  31. private function getPlayInfo($client, $videoId)
  32. {
  33. $request = new GetPlayInfoRequest();
  34. $request->setVideoId($videoId);
  35. $request->setAcceptFormat('JSON');
  36. $playConfig = ['PlayDomain' => 'http://oss.caihongxingqiu.net/'];
  37. $request->setPlayConfig(json_encode($playConfig));
  38. return $client->getAcsResponse($request);
  39. }
  40. }