PHP:SMS移动短信接口二次开发中的插入多行记录操作(含实时进度显示)
本来这个CP是单发的SMS接口,由于种种原因需要实现群发,每次编辑一条信息,群发网站所有会员(上万条会员记录),程序难点在从MYSQL到MSSQL不同数据库服务器间万行记录记录插入更新,并实时发送进度显示,反馈发送后的结果。
在短信发送过程中有以下几个要求:
1)不允许出现程序超时现象; 2)自动检测手机号码的合法性;3)计算发送的进度比例;
4)从MYSQL到MSSQL不同数据库服务器间万行记录记录插入更新,SQL语句不超负荷;
简单描述解决方法:
利用JQ 中一个AJAX脚本 递交 发送短信记录申请,程序读取申请记录,用SESSION记录下进度数据,根据每次发送的条数,返回记录中的未发送短信的手机号码,发送程序之前加手机合法性验证。AJAX接受到第一次返回记录数据,将手机号码提交给一个递归函数,再在函数中重复递交剩余的手机号码,直到进度为100%。
不知道我又没有说明白,参考代码如下:
function send_onesms(s,p,sendid){ //这个是第一个AJAX发送
if (sendid==''){
$.prompt('你还没有选择要发送的短信?',{buttons:{确定:true}});
return false;
}
var totel = '';
$('#loading').show();
$.ajax({
timeout: 36000,
type: "GET",
url: "/admincp/page.php",
data: "showtype=2&page="+p+'&sendid='+sendid,
beforeSend: function(){
$.ajax({async:true,url: "/admincp/jindu.php?act=clear&rnd="+new Date().getTime(), success: function(response){$("#jindu").html(response);}, dataType: "html"});
$('a.sendclass').html('锁定状态').removeAttr("onclick");
$('#sid'+sendid).html('发送中...').removeAttr("onclick");
},
success: function(msg){
$('#i2page').val=p;
//$('#smslist').html(unescape(msg));
//$('#loading').hide();
if (msg!='over'){
totel = msg;
// setInterval(function() { $.ajax({async:true,url: "/admincp/jindu.php?rnd="+new Date().getTime(), success: function(response){$("#jindu").html(response);}, dataType: "html"}) },1000);
}
}
});
send_sms(s,p,sendid,totel);
//window.clearInterval(thread);
}
function send_sms(s,p,sendid,totel){ //一个递归函数
$.ajax({
timeout: 36000,
dataType: "html",
type: "POST",
url: "/admincp/page.php?rnd="+new Date().getTime(),
data: "showtype=2&page="+p+'&sendid='+sendid+'&totel='+totel,
success: function(response){
if (response!='over'){
$.ajax({async:true,url: "/admincp/jindu.php?rnd="+new Date().getTime(), success: function(response){$("#jindu").html(response);}, dataType: "html"});
setTimeout(send_sms(s,p,sendid,response),1000);
}else{
$.ajax({async:true,url: "/admincp/jindu.php?act=over&rnd="+new Date().getTime(), success: function(response){alert(response);}, dataType: "html"});
$('#loading').hide();
menu_sec2_move(p);
}
}
});
}
============下面是PHP里的代码==========
function send_sms($sid='',&$msg,&$totel){
if ($totel=='' && $_SESSION['sendjindu'] == 0){ //第一次执行
if($this->get_tel($sid,$msg,$totel)){;
$telarr = explode(",",$totel);
$_SESSION['sendcount'] = count($telarr);
}else{
$_SESSION['sendjindu'] = 100;
}
$i = 0;
$j = 0;
$_SESSION['send_i'] = 0;
$_SESSION['send_j'] = 0;
}else{ // 进度刷新执行
if ($_SESSION['sendjindu'] == 100 ){
$msg = '成功:'.$_SESSION['send_i'].' ,错误:'.$_SESSION['send_j'];
return false;
}
$i = $_SESSION['send_i'];
$j = $_SESSION['send_j'];
}
if ($sid==''){
$msg='未知短信记录';
return false;
}
$db = new DB_Sql; //初始化数据库
$db->connect(DB_Database, DB_Host, DB_User, DB_Password); //数据库连接
$db->DB_Sql("set names gbk");
$sql ="select * from sz_sms where sd_state = '已经审核' and sd_id=".$sid;
$db->query($sql);
if ($db->next_record()){
$sdid=$db->Record['sd_id'];
$sdfrom=$db->Record['sd_from'];
$sdto=$db->Record['sd_to'];
$sdcontent=$db->Record['sd_content'];
$sdtime=$db->Record['sd_time'];
$sdstate=$db->Record['sd_state'];
$sdtowho=$db->Record['sd_towho'];
$sdediter=$db->Record['sd_editer'];
$sdpasser=$db->Record['sd_passer'];
$sdpasstime=$db->Record['sd_passtime'];
}else{
$msg='短信记录状态不属于已经审核';
return false;
}
$alljindu = $_SESSION['sendcount']; //总数
$telarr = explode(",",$totel);
$cpdb=mssql_connect(CP_Host,CP_User,CP_Password);
mssql_select_db(CP_Database,$cpdb);
$sendtime = date("Y-m-d H:i:s");
$t = 10; //每次发送几条,数值越大等待时间越长(注意:数值太大,客户端容易死机)
$c=0;
$nexttel='';
$sql = "";
foreach ($telarr as &$value) {
$sender = substr($value,0,11);
if ($c<$t){
if (preg_match("/(^(13)[0-9][0-9]{8}$)|(^(15)[0-9][0-9]{8}$)/",$sender)){
$sql = "INSERT INTO sms_table (from_telephone,to_telephone,msg_content,msg_time)VALUES ";
$sql .= "( '".$sdfrom."','".$sender."','".$sdcontent."','".$sendtime."')";
mssql_query($sql,$cpdb);
$i++;
}else{
$j++;
}
}else{
if ($nexttel ==''){
$nexttel = $sender;
}else{
if ($sender!='') $nexttel .= ','.$sender;
}
}
$c++;
}
$_SESSION['sendjindu'] = intval((($i+$j)/$alljindu)*100);
$_SESSION['send_i'] = $i ;
$_SESSION['send_j'] = $j;
if ($nexttel==''){$_SESSION['sendjindu'] = 100;}
$totel = $nexttel;
$msg = '成功:'.$i.' ,错误:'.$j;
return true;
}