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