在开始之前必须注册阿里云并且选好套餐开通短信服务(618或者双11一般都有活动).
然后在阿里云控制台中将accessKeyId、accessKeySecret获取并纪录起来,如果你的账户没有开启AccessKey需要进行开启操作:
1、登录阿里云控制台。
2、将鼠标放在右上方的用户名区域,在弹出的快捷菜单中选择accesskeys。
3、系统弹出安全提示对话框,单击继续使用AccessKey。页面显示AccessKeyId和AccessKeySecret。
如果创建了子用户需要在控制台中打开访问控制RAM->用户管理->设置中开通子用户管理短信服务(SMS)权限,否则无法发送短信.
接着需要申请好对应的短信模版及签名
然后开始代码工作:
- 下载阿里云短信服务的PHP版本SDK阿里云短信服务SDK for PHP
- 将下载的SDK包解压并复制其中的api_sdk包到你项目的extend目录下并重命名
- 在你重命名完成的这个包内新建一个php文件
- 在新建的php文件里创建发送短信的类和方法
<?php namespace alisms; use Aliyun\Core\Config; use Aliyun\Core\Profile\DefaultProfile; use Aliyun\Core\DefaultAcsClient; use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest; use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest; use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest; // 引入autoload.php,注意匹配成你自己的文件夹目录 require_once env('extend_path').'alisms\vendor\autoload.php'; // 加载区域结点配置 Config::load(); class Alisms { // 阿里云短信的accessKeyId public $access_key_id = '你的accessKeyId'; // 阿里云短信的accessKeySecret public $access_key_secret = '你的accessKeySecret'; // 短信签名,需要审核通过 public $sign_name = '你的短信签名'; // 短信模版ID,需要审核通过 public $template_code = null; public function send($phone,$template_param) { // 获取成员属性 $access_key_id = $this->access_key_id; $access_key_secret = $this->access_key_secret; $sign_name = $this->sign_name; $template_code = $this->template_code; // 产品名称:云通信短信服务API产品,开发者无需替换 $product = "Dysmsapi"; // 产品域名,开发者无需替换 $domain = "dysmsapi.aliyuncs.com"; // 暂时不支持多Region $region = "cn-hangzhou"; // 服务结点 $endPointName = "cn-hangzhou"; // 初始化用户Profile实例 $profile = DefaultProfile::getProfile($region,$access_key_id,$access_key_secret); // 增加服务结点 DefaultProfile::addEndpoint($region,$endPointName,$product,$domain); // 初始化AcsClient用于发起请求 $acs_client = new DefaultAcsClient($profile); // 初始化SendSmsRequest实例用于设置发送短信的参数 $request = new SendSmsRequest(); // 设置电讯接收号码 $request->setPhoneNumbers($phone); // 设置签名名称 $request->setSignName($sign_name); // 设置模版CODE $request->setTemplateCode($template_code); // 设置模版参数 if ($template_param) { $request->setTemplateParam(json_encode($template_param)); } // 发起访问请求 $acs_response = $acs_client->getAcsResponse($request); // 返回请求结果,数组格式 $result = json_decode(json_encode($acs_response),true); return $result; } }
- 在你项目或对应模块的common.php中封装好发送验证码短信的公共函数,使用redis缓存需要在config/catch.php中配置好redis
<?php return [ // 驱动方式 'type' => 'complex', 'default' => [ 'type' => 'file', 'expire' => 0, 'prefix' => '', 'path' => '', ], // 配置redis 'redis' => [ 'type' => 'redis', 'host' => '127.0.0.1', 'expire' => 0, 'prefix' => '', ], ];
use alisms\Alisms; use think\facade\Cache; /** * Notes: 发送短信验证码并保存到redis * User: Tidd Yan * Time: 2020/5/7 14:21 * @param int $phone 手机号 * @param string $template_code 阿里大于认证注册过的短信模版 * @return mixed 发送结果 */ if (!function_exists('sendCode')) { function sendCode($phone = 13345674567, $template_code = 'SMS_188445030') { // 实例化对象 $sms = new Alisms(); // 设置短信模版 $sms->template_code = $template_code; // 生成6位验证码 $code = mt_rand(100000, 999999); // 设置短信内容 $template_param = array('code' => $code); // 缓存短信内容 Cache::store('redis')->set('phone_' . $phone, $code, 300); // 发送短信 $result = $sms->send($phone, $template_param); return $result; } }
- 在控制器中调用公共函数发送短信
/** * Notes: 发送验证码短信 * User: Tidd Yan * Time: 2020/5/7 14:58 * @return \think\response\Json */ public function sendSMS() { $phone = input('phone'); // 验证码模版类型 $template_code = '申请的模版CODE'; // 发送验证码 $res = sendCode($phone, $template_code); if ($res['Code'] == 'OK') { return 'SUCCESS'; } else { return json($res); } }
- 获取redis中保存的验证码,判断验证码是否正确
Cache::store('redis')->get('phone_' . $phone)
OK,搞定,阿里大于还是很棒的,验证码秒到~