TencentCloudService.php 3.9 KB

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