阿里云发送短信
一、首先注册阿里云账号
二、开通短息服务
三、点击管理控制台
四、进行实名认证,然后点击【国内短信】,申请签名和模板
五、点击【帮助文档】,上面有详细讲解怎样接入短信的完整流程
六、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' //短信模板 阿里云后台添加 ], ];
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏