基于ThinkPHP3.2的短信接口

来源:互联网 发布:淘宝怎么清仓 编辑:程序博客网 时间:2024/05/22 06:17

1、在ThinkPHP/Library/下建一个Test/Send.class.php

<?php/*--------------------------------功能:     HTTP接口 发送短信类修改日期:   2015-02-26说明:     http://api.sms.cn/mt/?uid=用户账号&pwd=MD5位32密码&mobile=号码&mobileids=号码编号&content=内容状态:    100 发送成功    101 验证失败    102 短信不足    103 操作失败    104 非法字符    105 内容过多    106 号码过多    107 频率过快    108 号码内容空    109 账号冻结    110 禁止频繁单条发送    112 号码不正确    120 系统升级--------------------------------*/class Send{    function sendSMS($http,$uid,$pwd,$mobile,$content,$mobileids,$time='',$mid='')    {        $data = array            (            'uid'=>$uid,                   //用户账号            'pwd'=>md5($pwd.$uid),           //MD5位32密码,密码和用户名拼接字符            'mobile'=>$mobile,             //号码            'content'=>$content,           //内容            'mobileids'=>$mobileids,            'time'=>$time,                 //定时发送            'encode'=>'utf8',               //编码格式,看自己的项目需求了 我的是utf8的               );        $re= $this->postSMS($http,$data);          //POST方式提交        return $re;    }    function postSMS($url,$data='')    {        $port="";        $post="";        $row = parse_url($url);        $host = $row['host'];        $port = $row['port'] ? $row['port']:80;        $file = $row['path'];        while (list($k,$v) = each($data))        {            $post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码        }        $post = substr( $post , 0 , -1 );        $len = strlen($post);        $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);        if (!$fp) {            return "$errstr ($errno)\n";        } else {            $receive = '';            $out = "POST $file HTTP/1.1\r\n";            $out .= "Host: $host\r\n";            $out .= "Content-type: application/x-www-form-urlencoded\r\n";            $out .= "Connection: Close\r\n";            $out .= "Content-Length: $len\r\n\r\n";            $out .= $post;            fwrite($fp, $out);            while (!feof($fp)) {                $receive .= fgets($fp, 128);            }            fclose($fp);            $receive = explode("\r\n\r\n",$receive);            unset($receive[0]);            return implode("",$receive);        }    }}?>

2、在配置文件中写入

//短信配置         'message' => array(                            'http' => 'http://api.sms.cn/mtutf8/',  //申请的短信接口平台        'uid' => '***',                     //在云信使申请的短信验证用户账号(需要跟系统免费申请短信条数)          'pwd' => '*****',               //在云信使申请短信验证的用户密码        ),

js代码

$(".getcode").click(function(){                var tel= $("#phone").val();                $.ajax({                    url:"__URL__/verifyphone",                    type:"post",                    dataType:"json",                    async:true,                    data:{"tel":tel},                    success:function(database){                        if(database==1){                        }                    }                });            });

php代码

//验证手机号    public function verifyphone(){        $name = I("post.tel");        //调用发送短信方法        $this->phone($name);        $this->ajaxReturn(1);    }     /*    * microtime() 函数返回当前 Unix 时间戳和微秒数。    * mt_srand() 播种 Mersenne Twister 随机数生成器。从 PHP 4.2.0 版开始,seed 参数变为可选项,当该项为空时,会被设为随时数。    * 注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已自动完成。    * pow — 指数表达式    */    //random()这个函数是我用来生成一个随机数的,    //$numeric = 0生成一个6位的大小写字母与数字混合的字符串。$numeric = 1生成一个6位数字的字符串     public function random($length = 6 , $numeric = 1) {        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);        if($numeric) {            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));        } else {            $hash = '';            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';            $max = strlen($chars) - 1;            for($i = 0; $i < $length; $i++) {                $hash .= $chars[mt_rand(0, $max)];            }        }        return $hash;}    //发送短信的控制类为index    public function phone($name){        //以下三条读取配置文件中的内容即可        $http = C('message.http');        $uid = C('message.uid');        $pwd = C('message.pwd');        //要接受信息的手机号码,这里只是一个用于测试的手机号,找个项目需求操作即可        $mobile=$name;        //生成随机码        $random = $this->random(6,1);        //将值放入session 用于与输入的验证码进行验证        session($mobile,$random);//消息编号,该参数用于发送短信收取状态报告用,格式为消息编号+逗号;与接收号码一一对应,可以重复出现多次。        //这里只用一个编号即可,手机号加上微秒。        $mobileids = intval($mobile).microtime();        //要发送的内容        $content = urlencode("欢迎您注册,手机验证码:{$random},3分钟内有效!【上海市农业科学院】");        //即时发送,即:操作后就会进行发送,以下有定时发送        //调用封装好的短信接口类。        $_SESSION["cc"]="cc";        import('Test.Send');        $send = new \Send();        $res = $send->sendSMS($http,$uid,$pwd,$mobile,$content,$mobileids);    }
0 0
原创粉丝点击