PHP实现短信宝发送短信验证码功能

来源:互联网 发布:单片机开发智能家居 编辑:程序博客网 时间:2024/04/29 04:25

短信验证码是随机生成的几位数字,将此数字保存到数据库中,发送短信时将该数字 取出来放到下面代码填写短信内容处,即可发送。

注意要发送的手机号,短信宝账号、密码,短信发送内容等都要提前存放到数据库中,下面代码中从数据库中查询。

[php] view plain copy
  1. <?php  
  2. header("Content-type: text/html; charset=utf-8");  
  3. $statusStr = array(  
  4. "0" => "短信发送成功",  
  5. "-1" => "参数不全",  
  6. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",  
  7. "30" => "密码错误",  
  8. "40" => "账号不存在",  
  9. "41" => "余额不足",  
  10. "42" => "帐户已过期",  
  11. "43" => "IP地址限制",  
  12. "50" => "内容含有敏感词"  
  13. );  
  14. $smsapi = "http://api.smsbao.com/"; //短信网关  
  15. $user = "sdxx123"//短信平台帐号  
  16. $pass = md5("sdxx4567"); //短信平台密码  
  17. $content="此处填写短信内容";//要发送的短信内容  
  18. $phone = "15610537527";//要发送短信的手机号码  
  19. $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);  
  20. $result =file_get_contents($sendurl) ;  
  21. //短信发送后,可以将发送内容等记录到数据库中  
  22. // echo $statusStr[$result];  
  23. if($result=='0')  
  24. {  
  25.     //输出,短信发送成功  
  26.     echo "$statusStr[$result]";    
  27.     //此时可以更新发送状态  
  28. }else{  
  29.     //输出  短信发送失败原因  
  30.     echo "$statusStr[$result]";  
  31. }  
  32. ?> 




第二个版本

<?PHP

    header("Content-Type: text/html; charset=UTF-8");

    $flag = 0;
    $params='';//要post的数据
    $verify = rand(123456, 999999);//获取随机验证码        

    //以下信息自己填以下
    $mobile=$_POST['mobile'];//手机号
    $argv = array(
        'name'=>'18638784679',     //必填参数。用户账号
        'pwd'=>'8E81BDC879061D34C88EC7CF7E73',     //必填参数。(web平台:基本资料中的接口密码)
        'content'=>'验证码:'.$verify.',请勿将验证码提供给他人。',   //必填参数。发送内容(1-500 个汉字)UTF-8编码
        'mobile'=>$mobile,   //必填参数。手机号码。多个以英文逗号隔开
        'stime'=>'',   //可选参数。发送时间,填写时已填写的时间发送,不填时为当前时间发送
        'sign'=>'爱上秀',    //必填参数。用户签名。
        'type'=>'pt',  //必填参数。固定值 pt
        'extno'=>''    //可选参数,扩展码,用户定义扩展码,只能为数字
    );
    //print_r($argv);exit;
    //构造要post的字符串
    //echo $argv['content'];
    foreach ($argv as $key=>$value) {
        if ($flag!=0) {
            $params .= "&";
            $flag = 1;
        }
        $params.= $key."="; $params.= urlencode($value);// urlencode($value);
        $flag = 1;
    }
    $url = "http://web.cr6868.com/asmx/smsservice.aspx?".$params; //提交的url地址
    $con= substr( file_get_contents($url), 0, 1 );  //获取信息发送后的状态
    
    if($con == '0'){
        //yecw add start 20150821
        //校验码验证
        $mobile = $_R ['mobile'];
        $sql = "insert into pre_hf_msg (mobile,code,sttime) values ($mobile,$verify,NOW())";
        $re = db_factory::execute ( $sql );
    
        $result = array('ret'=>0,'message'=>'发送成功','data'=>'');
        echo json_encode($result);
        die();
    }else{
        //echo "<script>alert('发送失败!');history.back();</script>";
        $result = array('ret'=>-100,'message'=>'发送失败','data'=>'');
        echo json_encode($result);
        die();
    }
    
?>

0 0
原创粉丝点击