中国网建发送验证码

首先要设置中国网建的签名(没设置是不能用的):

之后拿发送时需要的   uid  和  key

接下来就可以开始写代码了

把这两个方法写到common.php文件中,我用的是 fastadmin 框架(thinkphp框架都可以写到common里),可以在其他地方直接调用


if (!function_exists('sendSms')) {
/**
* 发短信
* @param $mobile int 手机号码
* @param $content string 短信内容
* @return array
*/
function sendSms($mobile,$content){
$uid = '******'; //上面拿到的 uid
$pwd = '*********'; //上面拿到的 key
$url='http://utf8.sms.webchinese.cn/?Uid='.$uid.'&Key='.$pwd.'&smsMob='.$mobile.'&smsText='.$content;
$string = smsCURL($url);
if ($string>0) { //发送成功
$data=array(
'code'=>1,
'msg'=>"短信发送成功",
);
}else{
$data=array(
'code'=>0,
'msg'=>"短信发送失败",
);
}
return $data;
}
}

if (!function_exists('smsCURL')) {
/**
* curl
* @param $url string 请求地址
*/
function smsCURL($url=''){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
}

在common里写完两个方法后,找到   application/common/library文件夹下,找到  Sms.php,对里面的  send 方法进行改写。

/**
* 发送验证码
*
* @param int $mobile 手机号
* @param int $code 验证码,为空时将自动生成4位数字
* @param string $event 事件
* @return boolean
*/
public static function send($mobile, $code = null, $event = 'default')
{
$code = is_null($code) ? mt_rand(1000, 9999) : $code;
$content='本次短信验证码为【'.$code.'】,5分钟内有效';
$res = sendSms($mobile,$content); //调用刚写在 common 里的方法
if ($res['code'] == 1) { //发送成功
$time = time();
$ip = request()->ip();
$sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
if (!$sms) {
return false;
}else{
return true;
}
}else{
return false;
}
}

其他地方调用:

Sms::send($mobile, mt_rand(1000, 9999), "register");


调用示例:


/**
* 发送验证码
*
* @param string $mobile 手机号
* @param string $type 类型
*/
public function sendMsg()
{
$mobile = $this->request->param('mobile');
if (!$mobile) {
$this->error(__('Invalid parameters'));
}
   //进行正则校验
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
   //判断是否已被绑定或注册
$user = Db::name("user")->field("id")->where("mobile", $mobile)->find();
if (!empty($user)) {
$this->error("该手机号已被绑定");
}
   //调用发送验证码
if (Sms::send($mobile, mt_rand(1000, 9999), "register")) {
$this->success("发送成功");
} else {
$this->error("发送失败");
}
}

   /**
* 校验验证码
*
* @param string $mobile 手机号
* @param string $captcha 验证码
*/
public function checkCaptcha()
{
$mobile = $this->request->param("mobile");
$captcha = $this->request->param("captcha");
$type = "register";
if (!$captcha) {
$this->error(__('Invalid parameters'));
}
if (!Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect'));
}
$ret = Sms::check($mobile, $captcha, $type);
if (!$ret) {
$this->error(__('验证码错误或已过期'));
}
Sms::flush($mobile, $type);

$user = Db::name("user")->field("id")->where("mobile", $mobile)->find();
if (!empty($user)) {
$this->error("该手机号已被绑定");
}

if(Db::name("user")->where("id", $this->_user["id"])->setField("mobile",$mobile)){
$this->success("绑定成功");
}else{
$this->error("绑定失败");
}
}

猜你喜欢

转载自www.cnblogs.com/j-jian/p/11908368.html