php循环进程控制短信发送队列

进程文件,一分钟执行一次

background_deamon.php

<?php

/*
 * nohup /data/website/deamon/background_deamon.php > /data/website/deamon/nohup.out &
 * php /data/website/deamon/background_deamon.php &
 */
//single_worker();
//exit;

while (true){
	single_worker();
	sleep(1*60);
}

function single_worker(){
	$task_list = include('background_deamon_config.php');
	//var_export($task_list);
	date_default_timezone_set('Asia/Shanghai');
	
	$time = time();
	
	$_filehandle = fopen("background_deamon_".date('Y-m-d').".log","a+");
	
	foreach($task_list as &$vo){
		if ($vo['status'] == 0){
			continue;
		}
		
		$log_text = ""; 
		$url = $vo['url'];
		$updatetime = $vo['updatetime'];
		$log_text = "Ready to get ".$vo['url']."";
		
		if(!fwrite($_filehandle, date('Y-m-d H:i:s', time()) ."\t". $log_text ."\r\n" )){
			die("Failure to Log.");
		}
		
		$log_text = "";
		if(($time - $updatetime) >= $vo['interval']){
			$res = single_worker_url($url);
			//echo $url;
			//echo 'aaaa';
			$vo['updatetime'] = time();
			
			$log_text .= $res;
		}else{
			//没有到扫描时间
			$log_text .= "it is not in timer";
		}
		
		if(!fwrite($_filehandle, date('Y-m-d H:i:s', time()) ."\t". $log_text ."\r\n" )){
			die("Failure to Log.");
		}
	}
	
	fclose($_filehandle);
	
	$Setting = "<?php \n return ".var_export($task_list,TRUE).";?>";
	//var_export($Setting);
	$re = file_put_contents('background_deamon_config.php', $Setting);
}

function single_worker_url($url){
	//发起请求,获取内容
	//$url = "http://www.baidu.com";
	
	$counter = 0;
	
	$next = 0;
	do {
		$response_str = curlGet($url);//请求处理短信发送的接口
		
		if (strlen($response_str)){
			$response_array = json_decode($response_str, true);
		
			if ($response_array){
				$next = $response_array['next'];
			}
		}
		
		$counter = $counter + 1;
		
		sleep(1);
	}
	while ($next == 1);
	
	return "http request OK! (".$counter.")";
}

function curlGet($url){
	$ch = curl_init();
	$header = array(
			"Accept-Charset: utf-8",
	);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$temp = curl_exec($ch);
	return $temp;
}

function GetIP()
{
	if(!empty($_SERVER["HTTP_CLIENT_IP"]))
		$cip = $_SERVER["HTTP_CLIENT_IP"];
	else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
		$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	else if(!empty($_SERVER["REMOTE_ADDR"]))
		$cip = $_SERVER["REMOTE_ADDR"];
	else
		$cip = "Can not Get IP";
	return $cip;
}


background_deamon_config.php

<?php 
 return array (
  'test001' => 
  array (
    'url' => 'http://127.0.0.1/project01/index.php?g=Yanyubao&m=Deamon&a=background_sms_task&counter=5',
    'interval' => 60,
    'updatetime' => 1510052226,
    'memo' => '商户邀请短信发送进程',
    'status' => 1,
  )
);?>

 

发送短信接口

/**
	 * 发送短信消息的守护进程
	 * @param unknown $counter 为发送的最大条数
	 */
	public function background_sms_task($counter){
	
		$task_list = M('sms_task')->where(array('status' => 0))->order('create_time asc')->limit($counter)->select();
			
		//发短息
		require_once APP_PATH."Lib/ORG/TaobaoApi.class.php";//引入短信发送类(阿里大鱼)
		$appid = "";//appid
		$appsecert = "";//appsecert 
		$top_api = new TaobaoApi($appid, $appsecert);
		foreach ($task_list as $key=>$val){
			//修改短信队列表状态
			M('sms_task')->where(array('taskid'=>$val['taskid']))->setField(array('status'=>'1', 'updatetime'=>time(), 'updateip'=>get_client_ip()));
			
			$templateid = $val['templateid'];//短信模板id
			
				//批量邀请会员的短信模板
				$datalist = json_decode($val['datalist'],true);//json格式存储
				foreach ($datalist as $val1){
					
					
					$sms_template = '商户${sellername}邀请您成为门店会员,详情咨询${mobilephone},参与请到 http://xxxx.cn/${newid}';
					$sms_param = array();
					$sms_param['sellername'] = '张三';
					$sms_param['mobilephone'] = '13112341234';
					
					$sms_param_str = json_encode($sms_param);
					
					
				$resp =  $top_api->send_sms_msg("中国移动", $templateid, $val1['mobile'], $sms_param_str);
					//print_r($resp);
					Log::write("发送短信:".$sms_param_str);
					Log::write("发送短信返回结果:".print_r($resp, true));
			
			
		}
	}

测试时启动background_deamon.php

cmd切换到文件目录,找到php安装目录d:vertrigoserv/php/php background_deamon.php

example:

D:\dev\project01>  d:/vertrigoserv/php/php background_deamon.php

 

若要添加微信模板消息队列,大致思路一致,在background_deamon_config.php新增微信模板的接口地址,再补充详细代码即可

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值