- A+
所属分类:PHP
将文件下载,放到Thinkphp5的vendor目录下面;
在common.php中添加短信发送的函数:
/** * 阿里云短信 * @param $phone 手机号 * @param $code 验证码 */ function sendCode($phone,$code) { Vendor('Alisms.Core.Config'); Vendor('Alisms.Core.Profile.DefaultProfile'); Vendor('Alisms.Core.DefaultAcsClient'); Vendor('Alisms.Api.Sms.Request.V20170525.SendSmsRequest'); Vendor('Alisms.Api.Sms.Request.V20170525.QuerySendDetailsRequest'); \Aliyun\Core\Config::load();// 加载区域结点配置 $profile = \Aliyun\Core\Profile\DefaultProfile::getProfile(config('ALI_SMS.REGION'), config('ALI_SMS.KEY_ID'), config('ALI_SMS.KEY_SECRET'));// 初始化用户Profile实例 \Aliyun\Core\Profile\DefaultProfile::addEndpoint(config('ALI_SMS.END_POINT_NAME'), config('ALI_SMS.REGION'), config('ALI_SMS.PRODUCT'), config('ALI_SMS.DOMAIN'));// 增加服务结点 $acsClient = new \Aliyun\Core\DefaultAcsClient($profile);// 初始化AcsClient用于发起请求 $request = new \Aliyun\Api\Sms\Request\V20170525\SendSmsRequest();// 初始化SendSmsRequest实例用于设置发送短信的参数 $request->setPhoneNumbers($phone);// 必填,设置雉短信接收号码 $namecode = "XXXXX";// 必填,设置签名名称 $request->setSignName($namecode);// 必填,设置签名名称 $request->setTemplateCode('SMS_102310046');// 必填,设置模板CODE $params = array( 'code' => $code ); $request->setTemplateParam(json_encode($params));// 可选,设置模板参数 // 可选,设置流水号 //if($outId) { // $request->setOutId($outId); //} $acsResponse = $acsClient->getAcsResponse($request);// 发起访问请求 return $acsResponse; }
在application\config.php中设置阿里云短信账号信息:
'ALI_SMS' => [ 'PRODUCT' => 'Dysmsapi', 'DOMAIN' => 'dysmsapi.aliyuncs.com', 'REGION' => 'cn-hangzhou', 'END_POINT_NAME' => 'cn-hangzhou', 'KEY_ID' => 'XXXXXXXXXXX', 'KEY_SECRET' => 'XXXXxxXXXxxxXXXxxXXXXXXX' ],
使用阿里大鱼发送验证码:
//发送验证码 public function sendmsg(){ $phone = input('post.phone_num'); $code = rand(100000,999999); session("msgcode",$code); $start = time(); session('start',$start); $res = sendSms($phone,$code); if($code == "OK"){ $data = array('msg'=>'success'); }else{ $data = array('msg'=>'error'); } return json_encode($data); } //验证码验证 public function checkcode(){ if(!empty(Session::get('msgcode'))){ $msgcode = Session::get('msgcode'); $endtime = time(); $time = $endtime-Session::get('start'); session('start',NULL); if($time<= 120){ if($msgcode == input('post.msgcode')){ $data = array('data'=>'success'); } }else{ $data = array('data'=>'more'); } }else{ $data = array('data'=>'error'); } return json_encode($data); }