thinkphp3.2 发送短信验证码加判断

public function sms()
{
$phone = I('phone');//获取用户输入的手机号
$user = A('user')->get_my_info();      //获取登陆用户的信息
if (is_null($user) && !empty($user['user']['id'])) {//判断是否有这个用户
return $this->json_response(false, '没有此用户');
}
if (is_null($phone)) {//判断用户是否输入手机号
return $this->json_response(false, '手机号不能为空');
}
if (!preg_match("/^1[345789][\d]{9}$/", $phone)) {
return $this->json_response(false, '请输入合法的手机号');
}
if ($user['']['phone'] == $phone) {
return $this->json_response(false, '该手机号已经是您所绑定的手机号,请重新输入');
}
$phone_user = M('user')->where(array('phone' => $phone))->find();//判断用户输入的手机号是否已经绑定
if ($phone_user) {
return $this->json_response(false, '当前手机号已被使用,请换一个手机号绑定。');
}
$code = sprintf("%04d", rand(1, 9999));
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set($phone.$code,1800,$code);
$url = '';
$data = $this->getCurl($url);
$this->ajax_code(0, [], "url:" . $url . "\n response:" . $data);
}

 

private static function getCurl($url, $second = 30)
{
    $ch = curl_init();
    //设置超时
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
    //设置header
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //post提交方式
    //curl_setopt($ch, CURLOPT_POST, TRUE);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);


    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    set_time_limit(0);


    //运行curl
    $data = curl_exec($ch);
    //返回结果
    if ($data) {
        curl_close($ch);
        return $data;
    } else {
        $error = curl_errno($ch);
        curl_close($ch);
    }
}

URL需要自己填写拼接,get_my_info获取用户信息,不懂联系QQ:992660662

你可能感兴趣的:(php)