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

说明:

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

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


主要代码

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

[php]  view plain  copy
  1. <?  
  2. /*-------------------------------- 
  3. 功能:     PHP HTTP接口 发送短信 
  4. 修改日期:   2013-05-08 
  5. 说明:     http://m.5c.com.cn/api/send/?username=用户名&password=密码&mobile=手机号&content=内容&apikey=apikey 
  6. 状态: 
  7.     发送成功    success:msgid 
  8.     发送失败    error:msgid 
  9.  
  10. 注意,需curl支持。 
  11.  
  12. 返回值                                         说明 
  13. success:msgid                               提交成功,发送状态请见4.1 
  14. error:msgid                             提交失败 
  15. error:Missing username                      用户名为空 
  16. error:Missing password                      密码为空 
  17. error:Missing apikey                        APIKEY为空 
  18. error:Missing recipient                 手机号码为空 
  19. error:Missing message content               短信内容为空 
  20. error:Account is blocked                    帐号被禁用 
  21. error:Unrecognized encoding             编码未能识别 
  22. error:APIKEY or password error              APIKEY 或密码错误 
  23. error:Unauthorized IP address               未授权 IP 地址 
  24. error:Account balance is insufficient       余额不足 
  25. error:Black keywords is:党中央             屏蔽词 
  26. --------------------------------*/  
  27. function sendmsg($vcode,$mobile){  
  28.     $username = '*****';        //用户账号  
  29.     $password = '******';   //密码  
  30.     $apikey = '***********';    //api key  
  31.     //$mobile    = '18612345678,18988888888,18688888888';   //号手机码   
  32.   
  33.     //  var_dump($vcode);exit;  
  34.     $content = '您的短信验证码是:'.$vcode.'【湖南****信息科技有限公司】';       //内容  
  35.     //即时发送  
  36.     $result = sendSMS($username,$password,$mobile,$content,$apikey);  
  37.     if($result){  
  38.         return false;  
  39.     }else{  
  40.         return true;  
  41.     }  
  42.     //echo $vcode;  
  43.     //echo $result;  
  44.   
  45. }  
  46. function sendSMS($username,$password,$mobile,$content,$apikey)  
  47. {  
  48.     $url = 'http://m.5c.com.cn/api/send/?';  
  49.     $data = array  
  50.     (  
  51.         'username'=>$username,                   //用户账号  
  52.         'password'=>$password,               //密码  
  53.         'mobile'=>$mobile,                   //号码  
  54.         'content'=>$content,             //内容  
  55.         'apikey'=>$apikey,                   //apikey  
  56.     );  
  57.     $result= curlSMS($url,$data);           //POST方式提交  
  58.     return $result;  
  59. }  
  60.   
  61. function curlSMS($url,$post_fields=array()){  
  62.     $ch = curl_init();  
  63.     curl_setopt($ch, CURLOPT_URL,$url);  
  64.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  
  65.     curl_setopt($ch, CURLOPT_TIMEOUT, 3600); //60秒  
  66.     curl_setopt($ch, CURLOPT_HEADER,1);  
  67.     curl_setopt($ch, CURLOPT_REFERER,'http://www.xxoo.com');  
  68.     curl_setopt($ch,CURLOPT_POST,1);  
  69.     curl_setopt($ch, CURLOPT_POSTFIELDS,$post_fields);  
  70.     $data = curl_exec($ch);  
  71.     curl_close($ch);  
  72.     $res = explode("\r\n\r\n",$data);  
  73. //  return $res[2];  
  74. }  
  75. //$vcode = rand(1000,9999);  
  76. //$mobile = $_POST['mobile'];  
  77. //sendmsg($vcode,$mobile);  
  78. ?>  


2、控制器

[php]  view plain  copy
  1. //发送短信  
  2. import('Org.Msg');  
  3. $vcode=mt_rand(000000,999999);  
  4. $mobile=I('post.mobile');  
  5. $result=sendmsg($vcode,$mobile);//解释下参数: 参数1---验证码, 参数2----手机号;  
  6. if($result===false){  
  7.     //发送失败  
  8.     echo '发送短信失败,请重试';exit;  
  9. }else{  
  10.     //发送成功  
  11.     echo '短信验证码已发送';exit;  
  12. }  

大笑 有图有真相

猜你喜欢

转载自blog.csdn.net/xueshao110/article/details/80057453
今日推荐