84 lines
2.7 KiB
PHP
84 lines
2.7 KiB
PHP
<?php
|
|
namespace app\app\controller;
|
|
|
|
use AlibabaCloud\Client\AlibabaCloud;
|
|
use AlibabaCloud\Client\Exception\ClientException;
|
|
use AlibabaCloud\Client\Exception\ServerException;
|
|
use think\Controller;
|
|
|
|
class Smsaliyun extends Controller
|
|
{
|
|
|
|
// 阿里云短信配置
|
|
private $smsConfig = [
|
|
'accessKeyId' => 'LTAI5tQCdWe9Epir3ydXWbzp',
|
|
'accessKeySecret' => 'JKLzF0b5AXw2ajhwtem2fhPSUZVOZ5',
|
|
'signName' => '郑州巨天信息',
|
|
'templateCode' => 'SMS_484085215',
|
|
'regionId' => 'cn-hangzhou'
|
|
];
|
|
|
|
/**
|
|
* 发送短信接口
|
|
* @param string $phone 手机号
|
|
* @param string $code 验证码
|
|
*/
|
|
public function send_sms($phone='18530934717', $code='0932')
|
|
{
|
|
try {
|
|
// 初始化阿里云客户端
|
|
AlibabaCloud::accessKeyClient(
|
|
$this->smsConfig['accessKeyId'],
|
|
$this->smsConfig['accessKeySecret']
|
|
)
|
|
->regionId($this->smsConfig['regionId'])
|
|
->asDefaultClient();
|
|
|
|
// 发送短信请求
|
|
$result = AlibabaCloud::rpc()
|
|
->product('Dysmsapi')
|
|
->version('2017-05-25')
|
|
->action('SendSms')
|
|
->method('POST')
|
|
->host('dysmsapi.aliyuncs.com')
|
|
->options([
|
|
'query' => [
|
|
'RegionId' => $this->smsConfig['regionId'],
|
|
'PhoneNumbers' => $phone,
|
|
'SignName' => $this->smsConfig['signName'],
|
|
'TemplateCode' => $this->smsConfig['templateCode'],
|
|
'TemplateParam' => json_encode(['code' => $code]),
|
|
],
|
|
])
|
|
->request();
|
|
|
|
$result = $result->toArray();
|
|
|
|
if ($result['Code'] == 'OK') {
|
|
return [
|
|
'code' => 0,
|
|
'message' => '短信发送成功',
|
|
'data' => $result
|
|
];
|
|
} else {
|
|
return [
|
|
'code' => 99999,
|
|
'message' => $result['Message'],
|
|
'error' => $result
|
|
];
|
|
}
|
|
} catch (ClientException $e) {
|
|
return [
|
|
'code' => 99998,
|
|
'message' => '客户端异常: ' . $e->getErrorMessage(),
|
|
'error' => $e->getMessage()
|
|
];
|
|
} catch (ServerException $e) {
|
|
return [
|
|
'code' => 99997,
|
|
'message' => '服务端异常: ' . $e->getErrorMessage(),
|
|
'error' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
} |