You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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 {
// 实例化一个认证对象入参需要传入腾讯云账户secretIdsecretKey,此处还需注意密钥对的保密
$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;
}
}
}