TxySms.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\common\util;
  3. // 导入对应产品模块的client
  4. use TencentCloud\Sms\V20210111\SmsClient;
  5. // 导入要请求接口对应的Request类
  6. use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
  7. use TencentCloud\Common\Exception\TencentCloudSDKException;
  8. use TencentCloud\Common\Credential;
  9. // 导入可选配置类
  10. use TencentCloud\Common\Profile\ClientProfile;
  11. use TencentCloud\Common\Profile\HttpProfile;
  12. class TxySms
  13. {
  14. //发送短信
  15. public static function SendSms($template_code,$phone_all){
  16. try {
  17. /* 必要步骤:
  18. * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
  19. * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
  20. * 以免泄露密钥对危及你的财产安全。
  21. * SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
  22. $cred = new Credential(config('tencentcloud.SecretId'), config('tencentcloud.SecretKey'));
  23. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  24. $httpProfile = new HttpProfile();
  25. // 配置代理(无需要直接忽略)
  26. // $httpProfile->setProxy("https://ip:port");
  27. $httpProfile->setReqMethod("GET"); // post请求(默认为post请求)
  28. $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
  29. $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
  30. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  31. $clientProfile = new ClientProfile();
  32. $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
  33. $clientProfile->setHttpProfile($httpProfile);
  34. // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
  35. // 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
  36. $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
  37. // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
  38. $req = new SendSmsRequest();
  39. /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
  40. $req->SmsSdkAppId = config('sms.SdkAppId');
  41. /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
  42. $req->SignName = config('sms.sign');
  43. /* 模板 ID: 必须填写已审核通过的模板 ID */
  44. $req->TemplateId = config('sms.template_id');
  45. /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
  46. $req->TemplateParamSet = [$template_code];//数组多个参数
  47. /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
  48. * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
  49. $req->PhoneNumberSet = ['+86'.$phone_all];//数组可以发多个
  50. /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
  51. $req->SessionContext = "";
  52. /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
  53. $req->ExtendCode = "";
  54. /* 国际/港澳台短信 SenderId(无需要可忽略): 国内短信填空,默认未开通,如需开通请联系 [腾讯云短信小助手] */
  55. $req->SenderId = "";
  56. // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
  57. // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
  58. $resp = $client->SendSms($req);
  59. // 输出json格式的字符串回包
  60. $json_data = $resp->toJsonString();
  61. $arr_data = json_decode($json_data,true);
  62. return $arr_data;
  63. }
  64. catch(TencentCloudSDKException $e) {
  65. echo $e->getMessage();
  66. }
  67. }
  68. }