|
|
<?php
|
|
|
|
|
|
namespace tencent\message;
|
|
|
|
|
|
use TencentCloud\Common\Credential;
|
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
use TencentCloud\Sms\V20210111\SmsClient;
|
|
|
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
|
|
|
|
|
|
class Message extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 发送短信
|
|
|
* @param array $mobile_phone 下发手机号码,采用 e.164 标准,格式为+[国家或地区码][手机号],单次请求最多支持200个手机号且要求全为境内手机号或全为境外手机号。
|
|
|
* @param string $template_id 模板ID
|
|
|
* @param array $template_param 短信模板变量对应的实际值
|
|
|
* @param string $sign_name 短信签名内容,使用 UTF-8 编码,必须填写已审核通过的签名。国内短信为必填参数。
|
|
|
* @date 2022-11-28
|
|
|
*/
|
|
|
public function sendSms($mobile_phone, $template_id, $template_param = [], $sign_name = '')
|
|
|
{
|
|
|
try {
|
|
|
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
|
|
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
|
|
// 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
$httpProfile = new HttpProfile();
|
|
|
$httpProfile->setEndpoint("sms.tencentcloudapi.com");
|
|
|
|
|
|
// 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
$clientProfile = new ClientProfile();
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
// 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
$client = new SmsClient($cred, "ap-beijing", $clientProfile);
|
|
|
|
|
|
// 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
$req = new SendSmsRequest();
|
|
|
|
|
|
$params = array(
|
|
|
// 数组
|
|
|
"PhoneNumberSet" => $mobile_phone,
|
|
|
"SmsSdkAppId" => $this->config['sdk_app_id'],
|
|
|
"SignName" => $sign_name,
|
|
|
"TemplateId" => $template_id,
|
|
|
"TemplateParamSet" => $template_param
|
|
|
);
|
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
|
|
// 返回的resp是一个SendSmsResponse的实例,与请求对象对应
|
|
|
$resp = $client->SendSms($req);
|
|
|
// 输出json格式的字符串回包
|
|
|
$resp = $resp->toJsonString();
|
|
|
|
|
|
$result = json_decode($resp, true);
|
|
|
|
|
|
// 记录日志
|
|
|
$uid = defined('UID') ? UID : '';
|
|
|
platformLog($params, $result, 'tencent_message_send_sms_uid_' . $uid);
|
|
|
|
|
|
return $result;
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
echo $e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|