前端验证码倒计时、后台发送验证码、创蓝短信接口

前端代码:倒计时




   
   


   
   


   
   



后端发送验证码代码:


/**
     * 发送验证码
     */
    public function getMobileCode(){
        header("content-type:text/html; charset=utf-8");
        $Mobile = $_POST ["mobile"];      //用户修改的手机号


        $type = trim($_POST["type"]); // 定义用来发送短信
        $type = empty($type)?"reg":$type; //短信模版代码
        if (!empty($type) && strlen($Mobile)==11){
            $Template = M("pagetemplate")->where(array("E_Type"=>$type))->cache(true,6000)->find();   //判断类型,发送验证码有多个地方使用到,比如找回密码,注册等
            if(empty($Template)) $this->jsonReturn(0, "短信类型异常!", '');
            if(!empty($Template['ID'])){
                $Code    = getCode(5);//验证码
                $sendstr = str_replace("0000", "", $Template["E_Template"]);  //发送验证码文本、替换、例子:你正在注册某某商城,验证码为0000,[某某商城]
                $result  = sendSMS($Mobile, $sendstr, 'true');  //调用创蓝短信方法
                $result = $this->execResult($result);  //处理返回值
                if($result[1] == "0") {  //返回的是一个数组、状态码 0 是成功
                    $seReCode = $Mobile . "," . $Code;
                    $_SESSION['MobileCode'] = $seReCode;
                    $this->jsonReturn(1, "发送成功!", '');
                }else{
                    $this->jsonReturn(0, "发送失败!", '');
                }
            }else{
                $this->jsonReturn(0, "异常、非法操作!", '');
            }
        }else{
            $this->jsonReturn(0, "异常、非法手机号!", '');
        }
    }


/**
* 查询额度
*
* 查询地址
*/
protected function queryBalance() {
$chuanglan_config = $this->GetInterfacecon ( 'message' );
// 查询参数
$postArr = array (
'account' => $chuanglan_config ["ConS"] ['USERID'] ['val'],
'pswd' => $chuanglan_config ["ConS"] ['PWD'] ['val'] 
);
$result = $this->curlPost ( $chuanglan_config ["ConS"] ['URLQ'] ['val'], $postArr );
return $result;
}

/**
* 处理返回值
*/
protected function execResult($result) {
$result = preg_split ( "/[,\r\n]/", $result );
return $result;
}

/**
* 通过CURL发送HTTP请求
*
* @param string $url
*         //请求URL
* @param array $postFields
*         //请求参数
* @return mixed
*/
protected function curlPost($url, $postFields) {
$postFields = http_build_query ( $postFields );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
$result = curl_exec ( $ch );
curl_close ( $ch );
// dump ( $result );
return $result;
}


/**
* 发送短信
*
* @param string $mobile
*         手机号码
* @param string $msg
*         短信内容
* @param string $needstatus
*         是否需要状态报告
* @param string $product
*         产品id,可选
* @param string $extno
*         扩展码,可选
*/
protected function sendSMS($mobile, $msg, $needstatus = 'false', $product = '', $extno = '') {

// 创蓝接口参数
$postArr = array (
'account' => '',//账号
'pswd' => '',//密码
'msg' => $msg, //发送内容
'mobile' => $mobile, //手机号
'needstatus' => $needstatus,
'product' => $product,
'extno' => $extno 
);
$result = $this->curlPost ( $chuanglan_config ["ConS"] ['URL'] ['val'], $postArr );
return $result;
}

官方文档:https://www.253.com/api-docs-5.html,状态码地址:https://www.253.com/api-docs-1.html

你可能感兴趣的:(php,mysql,web前端)