TencentCloudService.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\api\service;
  3. use TencentCloud\Common\Credential;
  4. use TencentCloud\Common\Exception\TencentCloudSDKException;
  5. use TencentCloud\Common\Profile\ClientProfile;
  6. use TencentCloud\Common\Profile\HttpProfile;
  7. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  8. use TencentCloud\Sms\V20210111\SmsClient;
  9. use TencentCloud\Vms\V20200902\Models\SendTtsVoiceRequest;
  10. use TencentCloud\Vms\V20200902\VmsClient;
  11. use think\Env;
  12. // 导入 VMS 的 client
  13. class TencentCloudService extends BaseService
  14. {
  15. public static $MASSAGE_APPLY = 1913772;
  16. public static function tencent_cloud_sms_send($templateId, $mobile, $params)
  17. {
  18. try {
  19. $cred = new Credential(
  20. Env::get("sms.tencent_cloud_secret_id"),
  21. Env::get("sms.tencent_cloud_secret_key")
  22. );
  23. $httpProfile = new HttpProfile();
  24. $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
  25. $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
  26. $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
  27. $clientProfile = new ClientProfile();
  28. $clientProfile->setSignMethod("TC3-HMAC-SHA256");
  29. $clientProfile->setHttpProfile($httpProfile);
  30. $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
  31. $req = new SendSmsRequest();
  32. $req->SmsSdkAppId = config("site.tencent_cloud_sms_sdk_app_id");
  33. $req->SignName = config("site.tencent_cloud_sms_sign_name");
  34. $req->TemplateId = $templateId;
  35. $req->TemplateParamSet = $params;
  36. $req->PhoneNumberSet = array("+86{$mobile}");
  37. // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
  38. // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
  39. $resp = $client->SendSms($req);
  40. $resp_data = $resp->toJsonString();
  41. if (false === $resp_data)
  42. return new \SResult(0, "请求发送失败");
  43. $fmt_data = json_decode($resp_data, true);
  44. return new \SResult("Ok" === $fmt_data["SendStatusSet"][0]["Code"], "请求发送失败!", $fmt_data);
  45. } catch (TencentCloudSDKException $e) {
  46. return new \SResult(0, $e->getMessage());
  47. }
  48. }
  49. public static function tencent_cloud_vms_send($mobile, $template_id, $params = null)
  50. {
  51. try {
  52. $cred = new Credential(
  53. config("site.tencent_cloud_secret_id"),
  54. config("site.tencent_cloud_secret_key")
  55. );
  56. $httpProfile = new HttpProfile();
  57. $httpProfile->setReqMethod("POST");
  58. $httpProfile->setReqTimeout(30);
  59. $httpProfile->setEndpoint("vms.tencentcloudapi.com");
  60. $clientProfile = new ClientProfile();
  61. $clientProfile->setSignMethod("TC3-HMAC-SHA256");
  62. $clientProfile->setHttpProfile($httpProfile);
  63. $client = new VmsClient($cred, "ap-guangzhou", $clientProfile);
  64. $req = new SendTtsVoiceRequest();
  65. $req->TemplateId = $template_id;
  66. if (!is_null($params)) {
  67. $req->TemplateParamSet = $params;
  68. }
  69. $req->CalledNumber = "+86{$mobile}";
  70. $req->VoiceSdkAppid = config("site.tencent_cloud_vms_sdk_app_id");
  71. $req->PlayTimes = 2;
  72. $resp = $client->SendTtsVoice($req);
  73. $resp_data = $resp->toJsonString();
  74. if (false === $resp_data)
  75. return new \SResult(0, "请求发送失败");
  76. $fmt_data = json_decode($resp_data, true);
  77. return new \SResult(isset($fmt_data["RequestId"]), "请求发送失败!");
  78. } catch (TencentCloudSDKException $e) {
  79. return new \SResult(0, $e->getMessage());
  80. }
  81. }
  82. }