China building a network to send a verification code

We must first set up China built the network's signature (not set can not be used):

 

 

 

 

After the time needed to take the key and send uid

 

 

 

 

 

 

Then you can start writing code

 

These two methods written common.php file, I use the fastadmin frame (thinkphp frame can be written in common), you can directly call elsewhere


IF (! function_exists ( 'sendSms')) {
/ **
* texting
* @param $ mobile int phone number
* @param $ content string message content
* @return Array
* /
function sendSms ($ Mobile, $ Content) {
$ uid = '******'; // get above UID
$ pwd = '***************************************'; // get the above Key
$ = URL 'HTTP: // UTF8. sms.webchinese.cn/?Uid='.$uid.'&Key='.$pwd.'&smsMob='.$mobile.'&smsText='.$content;
$ String = smsCURL ($ URL);
IF ($ string> 0) {// sent successfully
$ = Data Array (
'code' =>. 1,
'MSG' => "send messages successfully"
);
}else{
$data=array(
'code'=>0,
'msg' => "SMS failed",
);
}
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;
}
}

 

After the two finished in the common method, found under the application / common / library folder, find Sms.php, to send a method which will be rewritten.

/ ** 
* Send Verification Code
*
* @param int $ Mobile phone number
* @param int $ code verification code is empty will automatically generate four digital
* @param string $ event event
* @return boolean
* /
public static function Send ($ Mobile, $ code = null, $ Event = 'default')
{
$ code = is_null to ($ code) the mt_rand (1000, 9999):? $ code;
$ Content = "The message authentication code [ '. . $ code '], 5 minutes valid';
$ sendSms RES = (Mobile $, $ Content); // call was just written in the common method
if ($ res [ 'code' ] == 1) {// sent successfully
$ Time = Time ();
$ IP = Request () -> IP ();
$ = SMS \ App \ Common \ Model \ the Sms :: Create ([ 'Event' => $ Event, 'Mobile' => $ mobile, 'code' => $ code, 'ip' => $ ip, 'createtime'=> $time]);
if (!$sms) {
return false;
}else{
return true;
}
}else{
return false;
}
}

 

Elsewhere call:

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


Call:


/ **
* Send code
*
* @param Mobile phone No. String $
* @param string $ type type
* /
public SENDMSG function ()
{
$ Mobile = $ this-> request-> param ( 'Mobile');
IF ( ! $ Mobile) {
$ this-> error (__ ( 'Invalid Parameters'));
}
   // for regular checking
if (! Validate :: regex ($ mobile, "^ 1 \ d {10} $")) {
$ this-> error (__ ( 'here Incorrect Mobile iS'));
}
   // determines whether bound or register
$ user = Db :: name ( " user") -> field ( "id") -> the WHERE ( "Mobile", $ Mobile) -> the Find ();
IF (! empty ($ the User)) {
$ this-> error ( "the phone number has been bound");
}
   // Send code call
if (Sms :: send ($ mobile , mt_rand (1000, 9999), "register")) {
$ this-> success ( "sent successfully");
} the else {
$ this-> error ( "transmission failed");
}
}

   /**
* 校验验证码
*
* @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 (__ ( 'code is incorrect or expired'));
}
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("绑定失败");
}
}

 

Guess you like

Origin www.cnblogs.com/j-jian/p/11908368.html