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.

97 lines
4.0 KiB

<?php
namespace app\utility;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
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')
->action('SendMessageToGlobe') // 该接口不支持往中国内地发送短信, QPS 限制为 2000 次/秒。超过限制,API 调用会被限流
->method('POST')
->options([
'query' => [
"To" => $toNum,
"Message" => $content,
],
])
->request();
$res = $result->toArray();
if (!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK') {
trace('短信发送失败:' . json_encode([$res]), 'error');
}
return true;
} catch (ClientException $clientException) {
trace('短信发送失败01_' . $clientException->getMessage(), 'error');
} catch (ServerException $serverException) {
trace('短信发送失败02_' . $serverException->getErrorMessage(), 'error');
} catch (\Exception $exception) {
trace('短信发送失败03_' . $exception->getMessage(), 'error');
}
return false;
}
// 发全球地区(除中国大陆地区)
public function sendMessageToGlobe($toNum, $content, $from, $accessKey, $secret)
{
try {
// 新对接的短信平台
$sms = new \app\sms\Sms();
$result = $sms->send($toNum, '', [
'content' => $content,
'title' => $from,
]);
trace('短信发送结果:' . json_encode([$result]), 'error');
if (!$result['success']) {
trace('短信发送失败:' . json_encode($result), 'error');
return false;
}
// AlibabaCloud::accessKeyClient($accessKey, $secret)
// ->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')
// ->action('SendMessageToGlobe')
// ->method('POST')
// ->host('dysmsapi.ap-southeast-1.aliyuncs.com')
// ->options([
// 'query' => [
// "To" => $toNum,
// "Message" => $content,
// "From" => $from, // Sender ID 在阿里云控制台申请
// ],
// ])
// ->request();
// $res = $result->toArray();
// trace('短信发送结果:' . json_encode([$res]), 'error');
// if (!isset($res['ResponseCode']) || $res['ResponseCode'] != 'OK') {
// return false;
// }
return true;
} catch (ClientException $clientException) {
trace('短信发送失败01_' . $clientException->getErrorMessage(), 'error');
} catch (ServerException $serverException) {
trace('短信发送失败02_' . $serverException->getErrorMessage(), 'error');
} catch (\Exception $exception) {
trace('短信发送失败03_' . $exception->getMessage(), 'error');
}
return false;
}
}