天天看点

PHP调用企信通短信平台接口

本篇文章介绍企信通(www.woxp.cn)短信平台接口的调用。

个人觉得这个平台接口的调用蛮简单的,也好用,不过得先申请好一个账号。

HTML代码:

<form action="{:U('Login/register')}" method="post">
	<div>
		<label>手 机 号 码</label>
		<input type="text" name="tel" id="form-mobile" maxlength="11" autocomplete="off" placeholder="输入手机号码 " οnkeyup="this.value=this.value.replace(/[^\d]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d]/g,'') " >
	</div>

	<div>
		<label>手机验证码</label>
		<input type="text" name="mobileCode" maxlength="6" id="phoneCode" placeholder="输入手机验证码" autocomplete="off">
		<button id="getPhoneCode" type="button">获取验证码</button>
	</div>
	<button type="submit" id="register">立即注册</button>
</form>
           

jQuery代码:

$(function(){
	$("#getPhoneCode").on("click",function(){
	        var _mobile = $.trim($("#form-mobile").val());

	        $.ajax({
	        	url:"{:U('Login/sendmobile')}",
	        	type:"post",
	        	dataType:"json",
	        	data:{mobile: _mobile},
	        	success:function(data){
	        		
	        		if(data.s==2){
	        			dialog({
						title: '提示',
						content: data.m,
					}).show();
	        		}
	        		if(data.s==3){
	        			dialog({
						title: '提示',
						content: data.m,
					}).show();
	        		}
			},
	        	error:function(){
				dialog({
					title: '提示',
					content: '网络异常,请稍后重试',
				}).show();
	        	}
	       	});


	});
});
           

PHP代码:

public function sendmobile($mobile){
		function is_mobile($mobile){
			return preg_match('/^1[34578]\d{9}$/', $mobile);
		}
		$r = is_mobile($mobile);
		if(!$r){
			exit(json_encode(array('s'=>3,'m'=>'不是合法的手机号码!')));
		}
		$code = rand(100000,999999);
		session('code',$code);			//把验证码保存到session里以便验证时校验
		
		$content = "您的验证码是:".$code.",有效期300秒【企业名】";
		$content = iconv("utf-8","gb2312//IGNORE",$content);
		
		//这里具体根据自己的需求填写,具体参数可参考接口文档:http://www.xhsms.com/jiekou.aspx  网站HTTP接口
		$url='http://gateway.woxp.cn:6630/gb2312/web_api/?x_eid=企业ID&x_uid=账号&x_pwd_md5=登陆密码MD5值&x_ac=10&x_gate_id=300&x_target_no='.$mobile.'&x_memo='.$content;
		function Get($url)
		{
			if(function_exists('file_get_contents'))
			{
				$file_contents = file_get_contents($url);
			}
			else
			{
				$ch = curl_init();
				$timeout = 5;
				curl_setopt ($ch, CURLOPT_URL, $url);
				curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
				$file_contents = curl_exec($ch);
				curl_close($ch);
			}
			return $file_contents;
		}
		if(Get($url)){
			//返回1为发送成功,发送成功后记录当前时间到session,验证时用当前时间减去发送时间判断是否大于300秒
			session('curtime',time());
			exit(json_encode(array('s'=>1,'m'=>'发送手机短信验证码成功!')));
		}else{
			exit(json_encode(array('s'=>2,'m'=>'发送手机短信验证码失败!')));
		}
	}
           

整个调用接口的过程就是这么简单,这是我的个人笔记,经过测试发送成功的,如果喜欢这篇文章就为我顶一下吧^w^.

如果你有什么问题可以留下评论,期待与你交流,互相学习。