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

来源:互联网 发布:linux关机 编辑:程序博客网 时间:2024/06/05 15:57

说明:

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

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


主要代码

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

<?/*--------------------------------功能:PHP HTTP接口 发送短信修改日期:2013-05-08说明:http://m.5c.com.cn/api/send/?username=用户名&password=密码&mobile=手机号&content=内容&apikey=apikey状态:发送成功success:msgid发送失败error:msgid注意,需curl支持。返回值说明success:msgid提交成功,发送状态请见4.1error:msgid提交失败error:Missing username用户名为空error:Missing password密码为空error:Missing apikeyAPIKEY为空error:Missing recipient手机号码为空error:Missing message content短信内容为空error:Account is blocked帐号被禁用error:Unrecognized encoding编码未能识别error:APIKEY or password errorAPIKEY 或密码错误error:Unauthorized IP address未授权 IP 地址error:Account balance is insufficient余额不足error:Black keywords is:党中央屏蔽词--------------------------------*/function sendmsg($vcode,$mobile){    $username = '*****';//用户账号    $password = '******';//密码    $apikey = '***********';//api key    //$mobile = '18612345678,18988888888,18688888888';//号手机码     //var_dump($vcode);exit;    $content = '您的短信验证码是:'.$vcode.'【湖南****信息科技有限公司】';//内容    //即时发送    $result = sendSMS($username,$password,$mobile,$content,$apikey);    if($result){        return false;    }else{        return true;    }    //echo $vcode;    //echo $result;}function sendSMS($username,$password,$mobile,$content,$apikey){    $url = 'http://m.5c.com.cn/api/send/?';    $data = array    (        'username'=>$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;}

大笑有图有真相