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.
60 lines
2.3 KiB
60 lines
2.3 KiB
<?php
|
|
|
|
namespace app\common\lib\sms\AliSms;
|
|
|
|
use AlibabaCloud\Client\AlibabaCloud;
|
|
use AlibabaCloud\Client\Exception\ClientException;
|
|
use AlibabaCloud\Client\Exception\ServerException;
|
|
class AliSms
|
|
{
|
|
public static function send(string $phone = null, int $code = null)
|
|
{
|
|
if (empty($phone) || empty($code)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
# 转换成json
|
|
$templateParam = json_encode(['code' => $code]);
|
|
|
|
// 创建客户端
|
|
AlibabaCloud::accessKeyClient(
|
|
config('alisms.accessKeyId'),
|
|
config('alisms.accessKeySecret')
|
|
)
|
|
->regionId(config('alisms.regionId'))
|
|
->asDefaultClient();
|
|
|
|
// 发送请求
|
|
$result = AlibabaCloud::rpc()
|
|
->product(config('alisms.product'))# 具体产品
|
|
->version('2017-05-25') # 指定版本不可修改
|
|
->action('SendSms')
|
|
->method('POST')
|
|
->host(config('alisms.host')) # host地址
|
|
->options([
|
|
'query' => [
|
|
# 指定区域
|
|
'RegionId' => config('alisms.regionId'),
|
|
//需要发送到那个手机
|
|
'PhoneNumbers' => $phone,
|
|
//必填项 签名(需要在阿里云短信服务后台申请)
|
|
'SignName' => config('alisms.SignName'),
|
|
//必填项 短信模板code (需要在阿里云短信服务后台申请)
|
|
'TemplateCode' => config('alisms.TemplateCode'),
|
|
//如果在短信中添加了${code} 变量则此项必填 要求为JSON格式
|
|
'TemplateParam' => $templateParam,
|
|
],
|
|
])
|
|
->request();
|
|
$sendRes = $result->toArray();
|
|
return $sendRes['Code'] == 'OK' ? ['status' => true, 'msg' => '发送成功'] : ['status' => false, 'msg' => '发送失败'];
|
|
} catch (ServerException $e) {
|
|
# 记录错误日志
|
|
return ['status' => false,'msg' => $e->getErrorMessage().'【1】'];
|
|
} catch (ClientException $e) {
|
|
# 记录错误日志
|
|
return ['status' => false,'msg' => $e->getErrorMessage().'【2】'];
|
|
}
|
|
}
|
|
}
|