ThinkPHP3.2.3整合发送手机短信验证码

说明:

本例使用的是美联软通的短信发送平台,网址是:http://web.5c.com.cn

每条价格在几分钱,买的量越大,优惠力度越大。


主要代码

1、在\ThinkPHP\Library\Org文件夹下,创建Msg.class.php文件,代码如下:

$username,					//用户账号
        'password'=>$password,				//密码
        'mobile'=>$mobile,					//号码
        'content'=>$content,				//内容
        'apikey'=>$apikey,				    //apikey
    );
    $result= curlSMS($url,$data);			//POST方式提交
    return $result;
}

function curlSMS($url,$post_fields=array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3600); //60秒
    curl_setopt($ch, CURLOPT_HEADER,1);
    curl_setopt($ch, CURLOPT_REFERER,'http://www.xxoo.com');
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post_fields);
    $data = curl_exec($ch);
    curl_close($ch);
    $res = explode("\r\n\r\n",$data);
//	return $res[2];
}
//$vcode = rand(1000,9999);
//$mobile = $_POST['mobile'];
//sendmsg($vcode,$mobile);
?>


2、控制器

//发送短信
import('Org.Msg');
$vcode=mt_rand(000000,999999);
$mobile=I('post.mobile');
$result=sendmsg($vcode,$mobile);//解释下参数: 参数1---验证码, 参数2----手机号;
if($result===false){
    //发送失败
    echo '发送短信失败,请重试';exit;
}else{
    //发送成功
    echo '短信验证码已发送';exit;
}

有图有真相

ThinkPHP3.2.3整合发送手机短信验证码_第1张图片


你可能感兴趣的:(ThinkPHP)