TrtcTencent.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Created by KLXQ
  4. * user: youyi
  5. * Date:2024/6/5 14:09.
  6. * Tencent TRTC SDK.
  7. * 目前主要用于云端录制、页面录制等相关API.
  8. */
  9. namespace App\Http\Helpers\Tencent;
  10. use App\Services\TencentRequestService;
  11. use TencentCloud\Common\Credential;
  12. use TencentCloud\Common\Exception\TencentCloudSDKException;
  13. use TencentCloud\Common\Profile\ClientProfile;
  14. use TencentCloud\Common\Profile\HttpProfile;
  15. use TencentCloud\Trtc\V20190722\TrtcClient;
  16. class TrtcTencent
  17. {
  18. public string $secretId;
  19. public string $secretKey;
  20. public string $region;
  21. public string $endpoint = 'trtc.tencentcloudapi.com';
  22. public object $client;
  23. public int $sdkAppId;
  24. public array $resultArr = ['code' => 200, 'msg' => 'success', 'data' => []];
  25. public function __construct($action, $params)
  26. {
  27. $this->secretId = config('tencent.tencent_cloud_secret_id');
  28. $this->secretKey = config('tencent.tencent_cloud_secret_key');
  29. $this->region = config('tencent.trtc.region');
  30. $this->sdkAppId = config('tencent.trtc.app_id');
  31. try {
  32. $cred = new Credential($this->secretId, $this->secretKey);
  33. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  34. $httpProfile = new HttpProfile();
  35. $httpProfile->setEndpoint($this->endpoint);
  36. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  37. $clientProfile = new ClientProfile();
  38. $clientProfile->setHttpProfile($httpProfile);
  39. // 实例化要请求产品的client对象,clientProfile是可选的
  40. $this->client = new TrtcClient($cred, $this->region, $clientProfile);
  41. $params['SdkAppId'] = $this->sdkAppId;
  42. // 实例化一个请求对象,每个接口都会对应一个request对象
  43. $respClass = 'TencentCloud\\'.ucfirst('trtc').'\\V20190722\\Models\\'.ucfirst($action).'Request';
  44. $req = new $respClass();
  45. $req->fromJsonString(json_encode($params));
  46. // 返回的resp是一个RemoveUserResponse的实例,与请求对象对应
  47. $resp = $this->client->{$action}($req);
  48. $this->resultArr['data'] = json_decode($resp->toJsonString(), true);
  49. } catch (TencentCloudSDKException $e) {
  50. $this->resultArr['code'] = -1;
  51. $this->resultArr['msg'] = $e->getMessage();
  52. }
  53. // 记录日志
  54. TencentRequestService::create('trtc', $action, $params, $this->resultArr);
  55. }
  56. }