详情见阿里云手册
首先安装SDK
composer require alibabacloud/client
引入
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
代码
/**
* @return \think\response\Json
* @throws ClientException
* 接收短信,发送验证码
*/
public function accept()
{
$phone = $this->request->param('phone');//接收电话号码
$result = [];
$code = mt_rand(100000,999999);//自定义验证码
//accessKeyId、accessSecret阿里云上获取
AlibabaCloud::accessKeyClient('accessKeyId', 'accessSecret')
->regionId('cn-hangzhou')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->host('dysmsapi.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'PhoneNumbers' => $phone,//电话
'SignName' => "",//名字
'TemplateCode' => "",//短信模板(阿里云上设置)
'TemplateParam' => "{\"code\":\"$code\"}",//验证码
],
])
->request();
$res = $result->toArray();
} catch (ClientException $e) {//ClientException
$res = $e->getErrorMessage();
} catch (ServerException $e) {//ServerException
$res = $e->getErrorMessage();
}
Cache::set('name_'.$phone, $code, 300);//设置缓存
return success([
'code' => $res
]);
}
发送完验证码接下来的操作就是再写一个方法,同时获取用户发送过来的验证码和缓存的验证码进行比较。
(做的笔记,自己看的)