|
|
@ -5,7 +5,6 @@ namespace app\utility; |
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
|
|
|
use AlibabaCloud\Client\Exception\ClientException; |
|
|
|
use AlibabaCloud\Client\Exception\ServerException; |
|
|
|
use think\facade\Log; |
|
|
|
|
|
|
|
class SendSms |
|
|
|
{ |
|
|
@ -32,10 +31,7 @@ class SendSms |
|
|
|
], |
|
|
|
]) |
|
|
|
->request(); |
|
|
|
|
|
|
|
$res = $result->toArray(); |
|
|
|
|
|
|
|
Log::error("----发送短信结果-----".json_encode([$res])); |
|
|
|
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){ |
|
|
|
trace('短信发送失败:'.json_encode([$res]), 'error'); |
|
|
|
} |
|
|
@ -51,44 +47,40 @@ class SendSms |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 发送中国大陆短信 【目前阿里云国际站账号中提示暂不支持发送国内短信】 |
|
|
|
public function sendMessageWithTemplate($to, $content, $accessKey, $secret) |
|
|
|
// 发全球地区(除中国大陆地区) |
|
|
|
public function sendMessageToGlobe($toNum, $content, $from, $accessKey, $secret) |
|
|
|
{ |
|
|
|
try { |
|
|
|
AlibabaCloud::accessKeyClient($accessKey, $secret) |
|
|
|
->regionId('cn-qingdao') |
|
|
|
->regionId('ap-southeast-1') // 服务点对应的公网接入地址: https://www.alibabacloud.com/help/zh/sms/developer-reference/api-dysmsapi-2018-05-01-endpoint?spm=a2c63.p38356.help-menu-44282.d_3_2_1.248f60deo2Ug5G |
|
|
|
->asDefaultClient(); |
|
|
|
|
|
|
|
$result = AlibabaCloud::rpc() |
|
|
|
->product('Dysmsapi') // 短信服务的产品名称 |
|
|
|
->version('2018-05-01') // API 版本 |
|
|
|
->action('SendMessageWithTemplate') // 该方法仅支持发送中国大陆地区短信 |
|
|
|
->product('Dysmsapi') |
|
|
|
->version('2018-05-01') |
|
|
|
->action('SendMessageToGlobe') |
|
|
|
->method('POST') |
|
|
|
->host('dysmsapi.aliyuncs.com') // 短信服务的域名 |
|
|
|
->host('dysmsapi.ap-southeast-1.aliyuncs.com') |
|
|
|
->options([ |
|
|
|
'query' => [ |
|
|
|
'RegionId' => 'cn-qingdao', // 区域 ID |
|
|
|
'PhoneNumbers' => $to, // 接收短信的手机号 多个号码用逗号分隔 |
|
|
|
'SignName' => $content, // 短信签名名称 短信签名名称,在阿里云控制台中创建。 |
|
|
|
'TemplateCode' => 'SMS_1234567', // 短信模板 ID, 短信模板 ID,在阿里云控制台中创建。 |
|
|
|
'TemplateParam' => json_encode(['code' => '123456']), // 模板参数(JSON 格式) 短信模板中的变量参数,以 JSON 格式传递(如验证码、动态内容等)。 |
|
|
|
"To" => $toNum, |
|
|
|
"Message" => $content, |
|
|
|
"From" => $from, // Sender ID 在阿里云控制台申请 |
|
|
|
], |
|
|
|
]) |
|
|
|
->request(); |
|
|
|
$res = $result->toArray(); |
|
|
|
|
|
|
|
Log::error("----国内发送短信结果-----".json_encode([$res])); |
|
|
|
trace('短信发送结果:'.json_encode([$res]), 'error'); |
|
|
|
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){ |
|
|
|
trace('国内短信发送失败:'.json_encode([$res]), 'error'); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} catch (ClientException $clientException) { |
|
|
|
trace('国内短信发送失败1 - '.$clientException->getErrorMessage(), 'error'); |
|
|
|
trace('短信发送失败01_'.$clientException->getErrorMessage(), 'error'); |
|
|
|
}catch (ServerException $serverException) { |
|
|
|
trace('国内短信发送失败2 - '.$serverException->getErrorMessage(), 'error'); |
|
|
|
trace('短信发送失败02_'.$serverException->getErrorMessage(), 'error'); |
|
|
|
}catch (\Exception $exception) { |
|
|
|
trace('国内短信发送失败3 - '.$exception->getMessage(), 'error'); |
|
|
|
trace('短信发送失败03_'.$exception->getMessage(), 'error'); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |