互亿短信验证码接口

2996

第一步:登陆http://www.ihuyi.com/官网注册一个会员账号(就会有10条信息免费发送测试用的....)

第二部:下载端口接口文件1458958712380363.png

下载下来就有1458958730129560.png

这些文件啦:这里是用thinkphp3.2.3来演示 选择php开发接口....

新建一个类或者控制器(因为演示就直接建立一个控制器):

random() 放在function.php中:
/**
 * 随机数
 * @param  integer $length  [description]
 * @param  integer $numeric [description]
 * @return [type]           [description]
 */
function random($length = 6 , $numeric = 0) {
    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;
}


控制器方法:
public function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
}
public function xml_to_array ($xml){
    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if(preg_match_all($reg, $xml, $matches)){
        $count = count($matches[0]);
        for($i = 0; $i < $count; $i++){
        $subxml= $matches[2][$i];
        $key = $matches[1][$i];
            if(preg_match( $reg, $subxml )){
                $arr[$key] = self::xml_to_array( $subxml );
            }else{
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}


//提交处理方法

public function secd () {
   $mobile =I('post.mobile'); //手机号啊
        //验证手机号码
        if (!$mobile || !preg_match("#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#", $mobile)) {
            $data = array('code' => 0,'msg' => '手机为空或者格式不正确');
            $this->ajaxReturn($data);
            exit();
        }
        //生成4位随机数
        $mobile_code = random(4,1); //4位
        //接口路径
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; //路径
        $name = C('HY_NAME');
        $pwd = md5(C('YH_PWD'));   

        $post_data = "account=".$name."&password=".$pwd."&mobile=".$mobile."&content=".rawurlencode("您的验证码是:".$mobile_code."。请不要把验证码泄露给其他人。");
        $gets =  self::xml_to_array(self::Post($post_data, $target));

        if($gets['SubmitResult']['code'] == 2) {
            session("mobile",$mobile); //手机号码
            session("mobile_code",$mobile_code); //手机验证码

        }
        echo $gets['SubmitResult']['msg'];
    }

前端页面代码:

<table width="100%"  border="0" align="left" cellpadding="5" cellspacing="3">
    <tr>
      <td align="right">手机<td>
    <input id="mobile" name="mobile" type="text" size="25" class="inputBg" /><span style="color:#FF0000"> *</span> 
    <input id="zphone" type="button" value=" 获取手机验证码 " onClick="get_mobile_code();"></td>
    </tr>
  </table>


//JS

 <script type="text/javascript">
 function get_mobile_code(){
        $.post('<{:U("Index/secd")}>', {mobile:$.trim($('#mobile').val()),send_code:<?php echo random(6,1);?>}, function(msg) {
            //alert($.trim(unescape(msg)));
                if( msg == '提交成功'){
                  RemainTime();
              }
        });
  };
  var iTime = 59;
  var Account;
  function RemainTime(){
    document.getElementById('zphone').disabled = true;
    var iSecond,sSecond="",sTime="";
    if (iTime >= 0){
      iSecond = parseInt(iTime%60);
      iMinute = parseInt(iTime/60)
      if (iSecond >= 0){
        if(iMinute>0){
          sSecond = iMinute + "分" + iSecond + "秒";
        }else{
          sSecond = iSecond + "秒";
        }
      }
      sTime=sSecond;
      if(iTime==0){
        clearTimeout(Account);
        sTime='获取手机验证码';
        iTime = 59;
        document.getElementById('zphone').disabled = false;
      }else{
        Account = setTimeout("RemainTime()",1000);
        iTime=iTime-1;
      }
    }else{
      sTime='没有倒计时';
    }
    document.getElementById('zphone').value = sTime;
  }
 </script>

短信接口演示完毕~~玄玄博客:http://www.blogxuan.com/