如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
默认是有10条免费短信的
html
<input type="button" value="发送验证码" class="send" id="message" onClick="sends.send();">
<script type="text/javascript">
var sends = {
send:function(){
var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0,6,7,8]{1})|(18[0-9]{1}))+\d{8})$/;
var nval =$("#phone").val().replace(/\s+/g,"");
if(!myreg.test(nval)){
Popup.open({
container : {
content : '请输入有效的手机号码!'
},
autoClose : 2000
});
}else if(nval.length==0){
Popup.open({
container : {
content : '请输入手机号码!'
},
autoClose : 2000
});
}else if(nval.length!=11){
Popup.open({
container : {
content : '请输入有效的手机号码!'
},
autoClose : 2000
});
}
if(myreg.test(nval)){
var url = "__APP__/public/userverify.html";
var item = $("#phone").val();
$.post(url,{item:item},function(data){
if(data.result==1){
layer.msg(data.msg);
// alert(11);
// window.location.href = data.url
}else{
layer.msg(data.msg);
}
},'json');
var time = 60;
var timer = setInterval(timeCountDown,1000);
function timeCountDown(){
if(time==0){
clearInterval(timer);
$('#message').removeClass('send1').val("重新获取验证码");
$('#message').removeAttr('disabled');
return true;
}else{
$('#message').addClass('send1').val(time+"S后再次发送");
$('#message').attr('disabled','true');
time--;
return false;
}
}
timeCountDown();
}
}
}
PHP方法
public function userverify() {
$str = "0123456789"; // 输出字符集
$n = 6; // 输出串长度
$len = strlen($str)-1;
$verify='';
for($i=0 ; $i<$n; $i++){
$verify.= $str[rand(0,$len)];
}
$item= $_POST['item'];
$msg = Check::rule(array(
array(check::mobile($item) && check::must($item), '请填写正确的手机号码!'),
));
if ($msg !== true) {
$this->jserror($msg);
}
$type = $_GET['type'];
if($item){
if(!empty($type)){
if (!empty($_SESSION['message_verify'.$item.$type])) {
$this->jserror('已经发送至您手机,请稍候再试');
}
}else{
if (!empty($_SESSION['message_verify'.$item])) {
$this->jserror('已经发送至您手机,请稍候再试');
}
}
if(!empty($type)){
$_SESSION['message_verify'.$item.$type] = $verify;
}else {
$_SESSION['message_verify'.$item] = $verify;
}
$_SESSION['verify']=$verify;
$sms = new sendsms();
$apikey = "b12a3c784ccc0000002bb02bc6481d7"; //请用自己的apikey代替
$mobile = $item; //请用自己的手机号代替
$text="【王明昌博客】您的验证码是".$verify."。如非本人操作,请忽略本短信";
$successsmg = $sms->send_sms($apikey,$text,$mobile);
/* 这里的$text要和$apikey 申请的保持一致,短信是发不出去的*/
if($successsmg){
$this->jssuccess('已经发送至您手机');
}
}else {
$this->jserror('获取验证码失败!');
}
}
sendsms.class.php
<?php
class sendsms{
/**
* 在PHP 5.5.17 中测试通过。
* 默认用通用接口(send)发送,若需使用模板接口(tpl_send),请自行将代码注释去掉。
*/
//通用接口发送样例
//模板接口样例(不推荐。需要测试请将注释去掉。)
/* 以下代码块已被注释
$apikey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; //请用自己的apikey代替
$mobile = "xxxxxxxxxxx"; //请用自己的手机号代替
$tpl_id = 1; //对应默认模板 【#company#】您的验证码是#code#
$tpl_value = "#company#=云片网&#code#=1234";
echo tpl_send_sms($apikey,$tpl_id, $tpl_value, $mobile);
*/
/**
* 通用接口发短信
* apikey 为云片分配的apikey
* text 为短信内容
* mobile 为接受短信的手机号
*/
function send_sms($apikey, $text, $mobile){
$url="http://yunpian.com/v1/sms/send.json";
$encoded_text = urlencode("$text");
$post_string="apikey=$apikey&text=$encoded_text&mobile=$mobile";
return $this->sock_post($url, $post_string);
}
/**
* 模板接口发短信
* apikey 为云片分配的apikey
* tpl_id 为模板id
* tpl_value 为模板值
* mobile 为接受短信的手机号
*/
function tpl_send_sms($apikey, $tpl_id, $tpl_value, $mobile){
$url="http://yunpian.com/v1/sms/tpl_send.json";
$encoded_tpl_value = urlencode("$tpl_value"); //tpl_value需整体转义
$post_string="apikey=$apikey&tpl_id=$tpl_id&tpl_value=$encoded_tpl_value&mobile=$mobile";
return $this->sock_post($url, $post_string);
}
/**
* url 为服务的url地址
* query 为请求串
*/
function sock_post($url,$query){
$data = "";
$info=parse_url($url);
$fp=fsockopen($info["host"],80,$errno,$errstr,30);
if(!$fp){
return $data;
}
$head="POST ".$info['path']." HTTP/1.0\r\n";
$head.="Host: ".$info['host']."\r\n";
$head.="Referer: http://".$info['host'].$info['path']."\r\n";
$head.="Content-type: application/x-www-form-urlencoded\r\n";
$head.="Content-Length: ".strlen(trim($query))."\r\n";
$head.="\r\n";
$head.=trim($query);
$write=fputs($fp,$head);
$header = "";
while ($str = trim(fgets($fp,4096))) {
$header.=$str;
}
while (!feof($fp)) {
$data .= fgets($fp,4096);
}
return $data;
}
}
?>
王明昌博客
