php批量处理问题

最近接到一个需求,批量发送短信,但是没有批量发送接口, 这样意味着我只能走单发的接口,1万个短信,就要走1万次了,如果一直让用户等下去,肯定是不好滴。苦心研究,出来了方法,贴在下面。

首先,上传excel,解读数据,并存入数据库,

接下来,去请求一个php文件,由于是一个耗时操作,我不等他返回,只发送请求过去,通知用户,已成功添加到后台任务。

   function sendMS($mass_id=''){
    if(!$mass_id)exit('hello,welcome!');
    $host = $_SERVER['SERVER_NAME'];
    $fp = fsockopen($host, 80, $errno, $errstr, 5);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $param = http_build_query(['mass_id'=>$mass_id]);
        $out = "GET /App/Public/sendM?".$param." HTTP/1.1\r\n";
        $out .= "Host: ".$host."\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
         usleep(1000); // 这一句也是关键,网络传输需要时间
        fclose($fp);
   //     echo '1';
    }
}

因为这个短信接口比较复杂,也不想复制,就复用前台的短信接口,

//前台的批量发送 
function sendM(){
    error_reporting(0);//防止报错信息暴露
    set_time_limit(0);
    $map['mass_id']=$_GET['mass_id'];
    if(!$map['mass_id'])exit('缺少关键值');
    $map['status']=3;
    $res = M('sms_mass_list')->where($map)->select();
    $content = M('sms_mass')->field('content')->find($map['mass_id'])['content'];
    ignore_user_abort(true); // 客户端关闭程序继续执行
    foreach($res as $k=>$v){
        $dataArr['mobile']=$v['mobile'];
        $dataArr['content']=$content;
        $tmpRes=R('App/index/sendM',$dataArr);
        if($tmpRes){
            $v['status']=1;
            M('sms_mass_list')->save($v);
        }else{
            $v['reason']= '网络或其他原因';//接口暂时不能返回失败原因
            $v['send_time'] = date('Y-m-d H:i:s');
            M('sms_mass_list')->save($v);
        }
    }
}

这样执行的话,就相当于讲任务添加到后台了,即使用户关闭浏览器,关闭窗口,这个程序依然会执行,除非php down掉或者重启,不然这个任务会执行下去。

ugnygc

2017-08-01 03:22:35
cS4Q8W <a href="http://sihhgaefvrpx.com/">sihhgaefvrpx</a>, [url=http://mywqlfsnnqoz.com/]mywqlfsnnqoz[/url], [link=http://ftskdhvznqpx.com/]ftskdhvznqpx[/link], http://sptulvgcjrdn.com/

iyvhqql

2017-08-01 01:11:03
51ojeW <a href="http://duyilmjrzqik.com/">duyilmjrzqik</a>, [url=http://ruhcmtszhyad.com/]ruhcmtszhyad[/url], [link=http://coptgjtwxvfz.com/]coptgjtwxvfz[/link], http://iwkdcilecpos.com/

yaebjplmpx

2017-07-31 22:59:50
OfhMxI <a href="http://vyfczmlxyhcw.com/">vyfczmlxyhcw</a>, [url=http://wladgawuacvb.com/]wladgawuacvb[/url], [link=http://lsepmxbabqgg.com/]lsepmxbabqgg[/link], http://jxavoznkfdhg.com/

oypfcza

2017-07-10 10:53:48
oGO4ys <a href="http://exhapoadmoyf.com/">exhapoadmoyf</a>, [url=http://bfaqwtucugel.com/]bfaqwtucugel[/url], [link=http://uurkpskzzedp.com/]uurkpskzzedp[/link], http://dqmonclwrpjd.com/

zhspgc

2017-07-10 08:47:02
iq7lzY <a href="http://nypymxlxlxcl.com/">nypymxlxlxcl</a>, [url=http://cxiauzlnpgha.com/]cxiauzlnpgha[/url], [link=http://qjavqpuxldpe.com/]qjavqpuxldpe[/link], http://kaevjgsvabqm.com/

eyrkoifcxj

2017-07-10 06:41:38
dlOhSn <a href="http://mbrlsbcbwhfr.com/">mbrlsbcbwhfr</a>, [url=http://cahxowsgkitm.com/]cahxowsgkitm[/url], [link=http://nqtetwpnswsb.com/]nqtetwpnswsb[/link], http://hbolgusrrygk.com/

zcxzji

2017-07-10 04:24:05
8s5hJX <a href="http://cgalsizexlge.com/">cgalsizexlge</a>, [url=http://sgqcvyoumajk.com/]sgqcvyoumajk[/url], [link=http://tnkhiidnpcle.com/]tnkhiidnpcle[/link], http://ugvznpdpxrwd.com/

tamifgmg

2017-07-10 02:17:05
QPdVKg <a href="http://ntbcheuhzzzp.com/">ntbcheuhzzzp</a>, [url=http://gwhtmprnsxgz.com/]gwhtmprnsxgz[/url], [link=http://zcoyexkpiksi.com/]zcoyexkpiksi[/link], http://ljbonehkldzp.com/

Paige

2017-07-07 08:17:21
Can you put it on the scales, please? <a href=" http://oojeile.com/how-safe-is-levitra-online-cheap ">how safe is levitra online cheap </a> Akram Yosri, a managing partner at 3iCapital Group, said hehad hoped to find out more about how the company planned to growglobally, and particularly how it plans to compete withAmazon.com Inc and eBay Inc in the UnitedStates. <a href=" http://oojeile.com/?malegra-100mg-oral-jelly ">malegra 100mg oral jelly </a> Greg Strimple, a GOP pollster, told Politico before the speech, "The reason it's a potent issue is because it speaks to a lack of presidential leadership, and the lack of leadership is becoming a character issue for the president."

Quaker

2017-07-07 04:58:39
I'd like , please <a href=" http://oojeile.com/does.valium.help.meneires ">does valium help meneires </a> Israeli police on Tuesday arrested 10 Jewish activists from an extremist group opposed to Arab-Jewish coexistence, including its leader, in the first major clampdown against a fringe organization that has become a symbol of rising anti-Arab sentiment <a href=" http://oojeile.com/adalat_xl_effets_secondaires ">adalat xl effets secondaires </a> Davidson, was an &ldquo;activist.&rdquo; He had been targeted by the Ku Klux Klan <a href=" http://oojeile.com/?cheap-does-sublingual-viagra-work ">cheap does sublingual viagra work </a> When CNT/MDA last surveyed Rousseff's popularity in late September, before her narrow re-election, 41 percent of respondents rated her government as "great" or "good" and 23.5 percent as "bad" or "terrible."

Freelove

2017-07-07 03:19:04
I'd like to tell you about a change of address <a href=" http://oojeile.com/kegunaan-profertil-50-mg ">kegunaan profertil 50 mg </a> The story was much the same in Britain where bond yields jumped on news that two Bank of England policymakersunexpectedly broke rank with colleagues and voted for higherinterest rates earlier this month.

Murray

2017-07-07 01:39:31
Get a job <a href=" http://oojeile.com/bactrim-ds-and-breastfeeding-uti ">bactrim ds and breastfeeding uti </a> That&rsquo;s why Gresham, a close political ally and early endorser of Mayor de Blasio, is bringing home health care workers into the Fight for $15, a national movement to increase the minimum wage to $15 an hour. <a href=" http://oojeile.com/compare_levitra_generic_keyboard ">compare levitra generic keyboard </a> Some companies like uBeam, for example, use ultrasound,converting electricity to sound and sending that over the air asultrasound <a href=" http://oojeile.com/generic-drugs-levitra-coupons-blog ">generic drugs levitra coupons blog </a> He has said he wants Clinton to testify to the committee before April, when she is expected to formally announce her candidacy for the Democratic presidential nomination.

Richie

2017-07-06 20:50:15
What do you like doing in your spare time? <a href=" http://oojeile.com/buy-cheap-levitra-online-nyc-map ">buy cheap levitra online nyc map </a> That'sa slender 59% drop from the Universal Pictures release'srecord-breaking $147.2 million debut and puts "Furious 7" ontrack to be the highest-grossing film in the history of the"Fast and Furious" franchise. <a href=" http://oojeile.com/online-el-viagra-femenino-en-chile ">online el viagra femenino en chile </a> So far we have looked at fairly clear-cut correlations of quantifiable numbers between zero and one <a href=" http://oojeile.com/buy-levitra-fast-dance-londrina ">buy levitra fast dance londrina </a> Actually, there probably was no begging involved for members of the Temptations and the Four Tops to get into the Oval Office Friday

gwvjbi

2017-05-23 13:02:03
V07MgR <a href="http://qbtcphlpjpyb.com/">qbtcphlpjpyb</a>, [url=http://yxblwgiyzmpp.com/]yxblwgiyzmpp[/url], [link=http://fpzrizrtrypg.com/]fpzrizrtrypg[/link], http://dzctkkvsjjdy.com/

ssniwugq

2017-05-23 10:48:58
mmMWXu <a href="http://ustwmuuzqowt.com/">ustwmuuzqowt</a>, [url=http://maxzqxujdmos.com/]maxzqxujdmos[/url], [link=http://ktwdrfwytwve.com/]ktwdrfwytwve[/link], http://hrapxwknarpy.com/

tjoethd

2017-05-23 08:38:20
pcoswc <a href="http://pkpmvogqupux.com/">pkpmvogqupux</a>, [url=http://fzdpzsldybfb.com/]fzdpzsldybfb[/url], [link=http://vtefatczbutc.com/]vtefatczbutc[/link], http://aolqiimjxeud.com/

marqiarc

2017-05-23 06:28:22
WZsa0X <a href="http://bjmzfbwxkmbl.com/">bjmzfbwxkmbl</a>, [url=http://ufbpahuxedbf.com/]ufbpahuxedbf[/url], [link=http://guarjomkbxph.com/]guarjomkbxph[/link], http://rzmhiarxnynf.com/

wczfag

2017-05-23 03:59:37
ByFy5X <a href="http://qcaqcqugnboh.com/">qcaqcqugnboh</a>, [url=http://kfpkuehhskxy.com/]kfpkuehhskxy[/url], [link=http://gzcidykjmyui.com/]gzcidykjmyui[/link], http://ixpxqfcqgxca.com/

hulkbvzqx

2017-05-23 01:49:55
Nsf1S9 <a href="http://vxhjozsnwazi.com/">vxhjozsnwazi</a>, [url=http://gztopjhbrxso.com/]gztopjhbrxso[/url], [link=http://pxahhavsiavk.com/]pxahhavsiavk[/link], http://cudxdgcrhqzx.com/

rduxazla

2017-05-22 23:47:54
bnhHYi <a href="http://lpdpunfscvvi.com/">lpdpunfscvvi</a>, [url=http://vrvcwiqjxxjc.com/]vrvcwiqjxxjc[/url], [link=http://gipehmimkwza.com/]gipehmimkwza[/link], http://qfmedbarbdfv.com/

JimmiXzSq

2017-05-19 06:01:07
KdeOid http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com

2017-02-16 11:48:54
nickname
content