Home>Article>PHP Framework> Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

wpj
wpj Original
2020-04-29 22:39:36 4023browse

如今,短信验证码已成为网站、APP的基础必备应用,应用场景十分丰富,随着移动互联网的发展会越来越多。作为一名码农,对第三方短信接口也是必须掌握的。本文php中文网将介绍使用工厂模式怎么实现ThinkPHP6.0接入阿里云短信。

一、环境要求

PHP版本 >= 7.1.0

开发环境必须安装有Composer

已开通阿里云短信服务,并且已获取AccessKey,创建模板和签名

最重要的,阿里云账户余额一定要有钱。

这里我就不演示开通短信服务和创建签名模板了,小伙伴们可以查看官方文档:https://help.aliyun.com/document_detail/108072.html?spm=a2c4g.11186623.6.565.1b4825903BoqGV

二、使用Composer安装Thinkphp6.0

如果您是第一次安装,请在命令行中切换到您的web目录执行下面的命令

composer create-project topthink/think sms

本教程将安装在C盘www目录下Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS三、使用Composer安装Alibaba Cloud SDK for PHP

进到刚刚创建的sms项目下执行下面的命令

composer require alibabacloud/sdk

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS四、使用编辑器打开项目 ,并在config文件夹下创建sms.php配置文件来管理阿里短信配置信息

 'dysmsapi.aliyuncs.com', //AccessKey ID 'access_key_id' => '您的AccessKey ID', //Access Key Secret 'access_key_secret' => '您的Access Key Secret', //地区ID 'region_id' => 'cn-hangzhou', //模板CODE 'template_code' => '您的模板CODE', //签名名称 'sign_name' => '您的短信签名名称', ];

五、顺便在config文件夹下打开cache.php添加Redis缓存配置,后面发送短信验证码会用到

 env('cache.driver', 'redis'), // 缓存连接方式配置 'stores' => [ 'file' => [ // 驱动方式 'type' => 'File', // 缓存保存目录 'path' => '', // 缓存前缀 'prefix' => '', // 缓存有效期 0表示永久缓存 'expire' => 0, // 缓存标签前缀 'tag_prefix' => 'tag:', // 序列化机制 例如 ['serialize', 'unserialize'] 'serialize' => [], ], // Redis缓存 'redis' => [ //服务器地址 'host' => '127.0.0.1', //redis端口 'port' => 6379, //驱动方式 'type' => 'redis', //缓存前缀 'prefix' => 'sms_code_', ] ], ];

六、在app目录下创建common/lib/sms/Sms.php接口类,用来约束发送短信验证码的方法


      

七、在common/lib/sms目录下创建AliSms类来实现Sms接口的smsSend()

regionId(config('sms.region_id'))->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host(config('sms.host')) ->options([ 'query' => [ 'RegionId' =>config('sms.region_id'), 'SignName' => config('sms.sign_name'), 'PhoneNumbers' => $phone, 'TemplateCode' => config('sms.template_code'), 'TemplateParam' => json_encode(['code' => $code]), ], ])->request(); } catch (ClientException $e) { return false; } catch (ServerException $e) { return false; } return true; } }

八、在common\lib目录下创建生成短信验证码的类Code.php


      

九、在common目录下创建service/Sms.php


      

十、在common目录下创建validate/SmsValidate验证器

 'require|mobile', 'code' => 'require|number' ]; //错误信息 protected $message = [ 'phone.require' => '请输入手机号', 'phone.mobile' => '手机号格式错误', 'code.require' => '短信验证码不能为空', 'code.number' => '短信验证码必须为纯数字' ]; //验证场景 protected $scene = [ 'sendCode' => ['phone'] ]; }

十一、在controller目录下创建Sms.php

isPost()){ //获取手机号 $data = [ 'phone' => request()->param('phone','','trim'), ]; //参数校验 $validate = new SmsValidate(); if (!$validate->scene('sendCode')->check($data)){ return json(['code'=>0,'msg'=>$validate->getError()]); } //发送短信验证码 if (\app\common\Service\Sms::sendCode($data['phone'],6,'AliSms')){ return json(['code'=>1,'msg'=>'发送成功,请注意查收。']); }else{ return json(['code'=>0,'msg'=>'发送失败,请稍后重试!']); } } } }

十二、使用PostMan测试发送短信验证码

不输入手机号或输入错误手机号会给相应的提示。

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

输入正确的手机号,提示发送短信验证成功Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS这时我们来看看收到的验证码和缓存中的验证码是否一致Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS到这里我们的发送验证码已经完成

十三、这时我们需要校验验证码是否正确,在app\controller目录下创建Login.php

 request()->param('phone','','trim'), 'code' => request()->param('code','','trim'), ]; //参数校验 $validate = new SmsValidate(); if (!$validate->check($data)){ return json(['code'=>0,'msg'=>$validate->getError()]); } //从Redis中获取验证码 $redisCode = cache($data['phone']); //判断验证码是否正确 if (empty($redisCode)){ return json(['code'=>0,'msg'=>'验证码已过期,请重新发送!']); } if ($redisCode != $data['code']){ return json(['code'=>0,'msg'=>'验证码输入错误,请重新输入!']); } return '验证成功'; } }

十四、使用PostMan校验验证码是否正确

输入错误的验证码,会给出响应的提示

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

输入正确的验证码,提示验证成功Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

看到这里我相信很多小伙伴的验证码都已发送成功了。

不知道小伙伴们有没有发现文中有两处参数校验的代码相识度很高,在后续的thinkphp技术文章中我会对这个问题进行优化,感兴趣的小伙伴请关注。

The above is the detailed content of Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn