tencentcloudserver.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\common\server;
  3. use TencentCloud\Common\Credential;
  4. // 导入要请求接口对应的Request类
  5. use TencentCloud\Common\Exception\TencentCloudSDKException;
  6. use TencentCloud\Common\Profile\ClientProfile;
  7. use TencentCloud\Common\Profile\HttpProfile;
  8. // 导入可选配置类
  9. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  10. use TencentCloud\Sms\V20210111\SmsClient;
  11. class tencentcloudserver
  12. {
  13. public function getconfig()
  14. {
  15. $config = [
  16. "SecretId" => 'AKIDRhiSv84j8H9j25sOdbJq77rKWR6TZOwE',
  17. "SecretKey" => 'ziANIxLbbNt87pGQLzMSIr9AuObfWoth',
  18. 'region' => 'ap-beijing',
  19. 'endpoint' => 'ocr.tencentcloudapi.com',
  20. ];
  21. return $config;
  22. }
  23. /**
  24. * 发送短信
  25. *
  26. * @param [type] $appid
  27. * @param [type] $SignName
  28. * @param [type] $TemplateId
  29. * @param [type] $TemplateParamSet
  30. * @param [type] $PhoneNumberSet
  31. * @return void
  32. * @author wj
  33. * @date 2022-07-26
  34. */
  35. public function sendsms($appid, $SignName, $TemplateId, $TemplateParamSet, $PhoneNumberSet)
  36. {
  37. try {
  38. $config = $this->getconfig();
  39. $cred = new Credential($config['SecretId'], $config['SecretKey']);
  40. $httpProfile = new HttpProfile();
  41. $httpProfile->setReqMethod("GET");
  42. $httpProfile->setReqTimeout(30);
  43. $httpProfile->setEndpoint($config['endpoint']); // 指定接入地域域名(默认就近接入)
  44. $clientProfile = new ClientProfile();
  45. $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
  46. $clientProfile->setHttpProfile($httpProfile);
  47. $client = new SmsClient($cred, $config['region'], $clientProfile);
  48. $req = new SendSmsRequest();
  49. $req->SmsSdkAppId = $appid;
  50. $req->SignName = $SignName;
  51. $req->TemplateId = $TemplateId;
  52. $req->TemplateParamSet = $TemplateParamSet;
  53. /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
  54. * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
  55. $req->PhoneNumberSet = $PhoneNumberSet;
  56. /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
  57. //$req->SessionContext = "";
  58. /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
  59. //$req->ExtendCode = "";
  60. /* 国际/港澳台短信 SenderId(无需要可忽略): 国内短信填空,默认未开通,如需开通请联系 [腾讯云短信小助手] */
  61. //$req->SenderId = "";
  62. $resp = $client->SendSms($req);
  63. $respArray = $resp->serialize();
  64. $respArray = $respArray['SendStatusSet'][0];
  65. if ('Ok' != $respArray['Code']) {
  66. return backarr(0, $respArray['Message']);
  67. }
  68. return backarr(1, 'suceess');
  69. } catch (TencentCloudSDKException $e) {
  70. return backarr(0, $e->getMessage());
  71. }
  72. }
  73. }