阿里云发送短信
一、首先注册阿里云账号
二、开通短息服务
三、点击管理控制台
四、进行实名认证,然后点击【国内短信】,申请签名和模板
五、点击【帮助文档】,上面有详细讲解怎样接入短信的完整流程
六、php实现代码
首先要使用composer安装阿里云短信依赖
composer require alibabacloud/client
- <?php
- namespace app\index\controller;
- use think\Controller;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use think\Exception;
- class Message extends Controller
- {
- private $config;
- /**
- * 发送短信
- *
- * @return \think\response\Json
- * @throws ClientException
- * @throws Exception
- * @throws \think\exception\PDOException
- */
- public function send()
- {
- if (!request()->isPost()) {
- return tips(\'非法请求\');
- }
- $method = input(\'_method\', \'\', \'trim\');
- if (strtoupper($method) != "POST") {
- return tips(\'非法请求\');
- }
- $mobile = input(\'mobile\', \'\', \'trim\');
- try {
- $this->config = config(\'ali_sms\');
- AlibabaCloud::accessKeyClient($this->config[\'accessKeyId\'], $this->config[\'accessKeySecret\'])
- ->regionId(\'cn-hangzhou\')
- ->asDefaultClient();
- $code = rand(100000, 999999);
- $data = [
- \'fcontent\' => $code,
- \'fmobile\' => $mobile,
- \'ftype\' => 0,
- \'fstatus\' => 1,
- \'fadd_time\' => time()
- ];
- $id = db(\'mobile_apply\')->insert($data);
- $result = AlibabaCloud::rpc()
- ->product(\'Dysmsapi\')
- ->version(\'2017-05-25\')
- ->action(\'SendSms\')
- ->method(\'POST\')
- ->options([
- \'query\' => [
- \'PhoneNumbers\' => $mobile,
- \'SignName\' => $this->config[\'signName\'],
- \'TemplateCode\' => $this->config[\'templateCodeValidate\'],
- \'TemplateParam\' => json_encode([\'code\' => $code]),
- ],
- ])
- ->request();
- $res = $result->toArray();
- db(\'mobile_apply\')->where(\'fstatus\', $id)
- ->update([\'fstatus\' => 2, \'fmsg\' => $result->toJson()]);
- if ($res[\'Code\'] != "OK") {
- throw new Exception($result[\'Message\']);
- }
- return tips(\'发送成功\', 1, $res);
- } catch (ClientException $e) {
- return tips($e->getErrorMessage());
- } catch (ServerException $e) {
- return tips($e->getErrorMessage());
- } catch (\Exception $e) {
- return tips($e->getMessage());
- }
- }
- /**
- * 验证短信
- *
- * @return array|string|\think\response\Json|true
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function valid()
- {
- if (request()->isPost()) {
- try {
- $mobile = input(\'mobile\', \'\', \'trim\');
- $code = input(\'code\', \'\', \'trim\');
- $fid = input(\'fid\', \'\', \'trim\');
- $this->config = config(\'ali_sms\');
- $sms = db(\'mobile_apply\')
- ->where([\'fmobile\' => $mobile, \'ftype\' => [\'eq\', 0], \'fstatus\' => 2])
- ->order(\'fadd_time desc\')
- ->find();
- if (!$sms) {
- throw new Exception(\'短信验证码已过期\');
- }
- if ((time() - $sms[\'fadd_time\']) > $this->config[\'expireTime\']) {
- throw new Exception(\'短信验证码已经过期\');
- }
- if ($code == $sms[\'fcontent\']) {
- db(\'mobile_apply\')->where([\'fmobile\' => $mobile, \'ftype\' => [\'eq\', 0]])->delete();
- db(\'account\')->where([\'fid\'=>$fid])->update([\'faccount_mobile\'=>$mobile]);
- return tips(\'验证成功\', 1);
- } else {
- return tips(\'验证失败\');
- }
- } catch (\Exception $e) {
- return tips($e->getMessage());
- }
- } else {
- return tips(\'非法请求\');
- }
- }
- }
配置文档
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- return [
- \'ali_sms\'=>[
- \'accessKeyId\'=>\'lalalalalall\',//阿里云后台申请
- \'accessKeySecret\'=>\'hahahahahahhah\',//阿里云后台申请
- \'expireTime\'=>600, // 5分钟 根据短信模板内容调整
- \'signName\'=>\'test商城\', // 短信签名 []中内容 阿里云后台添加
- \'templateCodeValidate\'=>\'SMS_169897105\' //短信模板 阿里云后台添加
- ],
- ];
版权声明:本文为ivy-zheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。