ThinkPHP3.2.3框架下接入阿里云短信服务接口实现:注册登录

第一步:下载PHP版本的[SDK轻量版]代码,解压,重命名为api文件夹名称,并将该文件夹存放在Thinkphp根目录位置(存放位置可以自己选择);

 

第二步,在根目录/Application/Common/Controller/创建控制层AlismsController.class.php

复制代码
accessKeyId = "AccessKeyId"; //AccessKeyId
        $this->accessKeySecret = "AccessKeySecret"; //AccessKeySecret
        $this->SignName = "短信签名"; //签名
        $this->CodeId = "短信验证码模板ID"; //验证码模板ID
    }
    
    //发送验证码
    public function code($phone,&$msg){
        
        if(!isphone($phone)){
            $msg = "手机号不正确";
            return false;
        }
        
        $params["PhoneNumbers"] = $phone; 
        $params["TemplateCode"] = $this->CodeId; //模板
        
        //记录存储验证码
        $code = rand(100000,999999);
        session("iphonecode",$phone.$code);//session存储手机号+验证码
        $params['TemplateParam'] = ["code" => $code]; //验证码
        
        return $this->send($params,$msg);        
    }
    
    //验证手机号是否正确
  private function isphone($phone){
       if (!is_numeric($phone)) {
            return false;
        }
        return preg_match("/^1[34578]{1}\d{9}$/", $phone) ? true : false;
    }

    //发送短信消息
    private function send($params=[],&$msg){
        
        $params["SignName"] = $this->SignName;
        
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
         $helper = new SignatureHelper();
        $content = $helper->request(
            $this->accessKeyId,
            $this->accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))
        );
        
        if($content===false){
            $msg = "发送异常";
            return false;
        }else{
            $data = (array)$content;
            if($data['Code']=="OK"){
                $msg = "发送成功";
                return true;
            }else{
                $msg = "发送失败 ".$data['Message'];
                return false;
            }
        }        
    }
}
复制代码

第三步,在根目录/Application/Admin/Controller/创建控制层LoginController.class.php

复制代码
code($_POST['iphone'],$msg);
       $this -> ajaxReturn($msg);
    }
}
?>
复制代码

 

另外:短信验证码60S倒计时及AJAX POST提交手机号JS如下:

复制代码

复制代码

其他HTML代码展示在这里就不在展示了;

 

转载于:https://www.cnblogs.com/jasonLiu2018/articles/10831080.html

你可能感兴趣的:(php,javascript)