p2 project
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.

94 lines
4.0 KiB

5 months ago
<?php
namespace app\utility;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\facade\Log;
class SendSms
{
public function send($toNum, $content, $accessKey, $secret)
{
try {
// LTAI5t66pUgcFJBMGHamFoxJ
// BxHnmVDiXvOrnxbAfOPvP6jibJL6pp
AlibabaCloud::accessKeyClient($accessKey, $secret)
->regionId('ap-southeast-1')
->asDefaultClient();
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
->host('dysmsapi.ap-southeast-1.aliyuncs.com')
->version('2018-05-01')
4 months ago
->action('SendMessageToGlobe') // 该接口不支持往中国内地发送短信, QPS 限制为 2000 次/秒。超过限制,API 调用会被限流
5 months ago
->method('POST')
->options([
'query' => [
"To" => $toNum,
"Message" => $content,
],
])
->request();
$res = $result->toArray();
Log::error("----发送短信结果-----".json_encode([$res]));
5 months ago
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){
trace('短信发送失败:'.json_encode([$res]), 'error');
5 months ago
}
return true;
}catch (ClientException $clientException) {
trace('短信发送失败01_'.$clientException->getMessage(), 'error');
}catch (ServerException $serverException) {
trace('短信发送失败02_'.$serverException->getErrorMessage(), 'error');
5 months ago
}catch (\Exception $exception) {
trace('短信发送失败03_'.$exception->getMessage(), 'error');
}
return false;
}
4 months ago
// 发送中国大陆短信 【目前阿里云国际站账号中提示暂不支持发送国内短信】
public function sendMessageWithTemplate($to, $content, $accessKey, $secret)
{
try {
AlibabaCloud::accessKeyClient($accessKey, $secret)
->regionId('cn-qingdao')
->asDefaultClient();
$result = AlibabaCloud::rpc()
->product('Dysmsapi') // 短信服务的产品名称
->version('2018-05-01') // API 版本
->action('SendMessageWithTemplate') // 该方法仅支持发送中国大陆地区短信
->method('POST')
->host('dysmsapi.aliyuncs.com') // 短信服务的域名
->options([
'query' => [
'RegionId' => 'cn-qingdao', // 区域 ID
'PhoneNumbers' => $to, // 接收短信的手机号 多个号码用逗号分隔
'SignName' => $content, // 短信签名名称 短信签名名称,在阿里云控制台中创建。
'TemplateCode' => 'SMS_1234567', // 短信模板 ID, 短信模板 ID,在阿里云控制台中创建。
'TemplateParam' => json_encode(['code' => '123456']), // 模板参数(JSON 格式) 短信模板中的变量参数,以 JSON 格式传递(如验证码、动态内容等)。
],
])
->request();
$res = $result->toArray();
Log::error("----国内发送短信结果-----".json_encode([$res]));
if(!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK'){
trace('国内短信发送失败:'.json_encode([$res]), 'error');
}
return true;
} catch (ClientException $clientException) {
trace('国内短信发送失败1 - '.$clientException->getErrorMessage(), 'error');
} catch (ServerException $serverException) {
trace('国内短信发送失败2 - '.$serverException->getErrorMessage(), 'error');
} catch (\Exception $exception) {
trace('国内短信发送失败3 - '.$exception->getMessage(), 'error');
}
}
5 months ago
}