如果我们是本地的php环境,那么定时任务怎么添加呢?那么就添加一个windows的定时任务吧。它同样具有linux下的crontab的效果。
首先准备文件:
例如我们现在有一个需要定时执行的文件:class_task.php
<?php
ini_set("display_errors", "on");
error_reporting(E_ERROR);
$_web_path_ = dirname(__FILE__).'/../';
$_ROOT_PATH_ = $_web_path_;
require $_web_path_.'lib/class/class.wx.php';
$wxClass = new weixin_class();
require $_web_path_."lib/dbconfig.php";
require $_web_path_."lib/core.php";
//引入PDO方式操作mysql
require $_web_path_ . 'lib/class/class.Model.php';
//引入第三方
require $_web_path_ . 'lib/class/third_platform/lib/extend/wxBizMsgCrypt.php';
require $_web_path_ . 'lib/class/third_platform/lib/ThirdWechatPlatform.php';
require $_web_path_.'lib/class/class.smsApi.php';
$third_config = require $_web_path_ . 'lib/class/third_platform/config/config.php';
importDbModel('member_multi_relation');
$m_member = new m_member_multi_relation();
$cn = mysql_connect(DB_HOST.":".DB_PORT, DB_USER, DB_PASS) or die('数据库连接错误');
mysql_select_db(DB_NAME,$cn);
mysql_query('set names utf8',$cn);
$taskQuery = mysql_query("select id, users_id, total from sc_class_task WHERE status=0 and is_del=0 order by id asc");
$size = 100;
while($row = mysql_fetch_array($taskQuery)) {
$total = $row["total"];
$pages = ceil($total / $size);
$quene_id = isset($row["id"]) ? $row["id"] : 0;
$uid = isset($row["users_id"]) ? $row["users_id"] : 0;
if($quene_id == 0 || $uid == 0) {
echo "quene_id或uid参数为0";
exit;
}
$sendtype = true;
for($i=1; $i<=$pages; $i++) {
$page = $i;
// $offset = ($page - 1)*$size;
$offset = 0;
$sendtype = sendExcuteFun($quene_id, $uid, $offset, $size, $third_config);
}
if($sendtype == true){
mysql_query("update sc_class_task set status=1 where id=" . $row["id"]);
}
}
file_put_contents('a88.log',date('Y-m-d H:i:s',time()).'执行了一次定时任务。'.PHP_EOL,FILE_APPEND);
function sendExcuteFun($quene_id, $uid, $offset, $size, $third_config = []) {
global $wxClass;
//取得当前的微信公众号
$query = mysql_query("select appid,appsecret from users_wx where users_id=".$uid." and wtype=2 and is_auth=1 ");
$appInfo = mysql_fetch_row($query);
if(!$appInfo) {
echo "微信公众号信息为空";
exit;
}
$datatime = time();
$query = mysql_query("select * from sc_class_task_quene where task_id=$quene_id and send_time < $datatime and status=0 order by id asc limit $offset,$size");
$sendtype = false;
//获取短信信息判断是否采用短信判断
$sms_info = M()->table('sms_school_admin_config')->where('users_id = ?', [$uid])->find();
if($sms_info){
$smsmApi = new smsApi($sms_info['user'],$sms_info['pwd']);
$sms_send_data = new stdClass();
}
while($row = mysql_fetch_array($query)) {
$sendtype = true;
$msgcontent = $row["content"];
$member_id = $row['member_id'];
$sms_type = 'remark';
if($sms_info){ //获取短信信息判断是否采用短信判断
//获取角色信息
$mtype = $row["mtype"];
$relation_mobile = '';//号码
switch ($mtype){
case 1:
$relation_result = M()->table('member_multi_relation','re')->join('sc_teacher', 'st', 'left')->on('re.rid=st.id AND st.is_del=0')->where('re.users_id = ? and re.member_group=? and re.member_id=?', [$uid,$mtype,$member_id])->find('st.tel');
$relation_mobile = $relation_result['tel'];
break;
case 2:
$relation_result = M()->table('member_multi_relation','re')->join('sc_student_parents', 'sd', 'left')->on('re.rid=sd.id AND sd.is_del=0')->where('re.users_id = ? and re.member_group=? and re.member_id=?', [$uid,2,$member_id])->find('sd.student_id');
$student_id = $relation_result['student_id'];
$student = M()->table('sc_student')->where('id=? and is_del=0',array($student_id))->find();
$relation_mobile = $student['tel'];
break;
case 0:
$relation_result = M()->table('member_multi_relation','re')->join('sc_student_parents', 'sd', 'left')->on('re.rid=sd.id AND sd.is_del=0')->where('re.users_id = ? and re.member_group=? and re.member_id=?', [$uid,2,$member_id])->find('sd.student_id');
$student_id = $relation_result['student_id'];
$student = M()->table('sc_student')->where('id=? and is_del=0',array($student_id))->find();
$relation_mobile = $student['tel'];
break;
case 3:
//M()->debug(true);
$relation_result = M()->table('member_multi_relation','re')->join('sc_student', 'sd', 'left')->on('re.rid=sd.id AND sd.is_del=0')->where('re.users_id = ? and re.member_group=? and re.member_id=?', [$uid,3,$member_id])->find('sd.tel');
$relation_mobile = $relation_result['tel'];
break;
}
if(!empty($relation_mobile) && is_mobile($relation_mobile) ){
$content = json_decode($msgcontent,true);
$sms_send_data->content = $content['data'][$sms_type]['value'];
$sms_send_data->mobile = $relation_mobile;
$result = $smsmApi->send($sms_send_data);
echo $result;
}
}
//获取公众号第三方信息()
$third = M()->table('wx_third_school_config')->where('users_id = ?', [$uid])->find();
if (!empty($third)) {
$thirdPlatform = ThirdWechatPlatform::instance($third_config);
$data = $thirdPlatform->sendWxMessage($uid, $msgcontent);
// file_put_contents(dirname(__FILE__)."/wxmsg_task_error_log_".date("Ymd", time()).".log", '[' . date('Y-m-d : h:i:sa', time()) . ']' . json_encode($third, JSON_UNESCAPED_UNICODE) . "\r\n", FILE_APPEND);
file_put_contents(dirname(__FILE__)."/wxmsg_task_error_log_".date("Ymd", time()).".log", '[' . date('Y-m-d : h:i:sa', time()) . ' =>third ]' . $data . "\r\n", FILE_APPEND);
} else {
$data = $wxClass->sendTemplateInfo($appInfo[0], $appInfo[1], $msgcontent);
}
$result = json_decode($data);
if($result->errcode == 0 && isset($result->msgid) && $result->msgid > 0) { //成功
mysql_query("update sc_class_task_quene set status=1, msg_id=".$result->msgid." where id=".$row['id']);
} else {
file_put_contents(dirname(__FILE__)."/class_task_error_log_".date("Ymd", time()).".log", json_encode($result), FILE_APPEND);
}
}
return $sendtype;
}
然后我们添加一个跟它同目录下的class_task.bat文件:
在class_task.bat中添加如下代码:
N:
"D:\phpstudy_pro\Extensions\php\php5.6.9nts\php.exe" -f "N:\phpStudy\WWW\www.367edu.com\services\class_task.php"
解释:D:\phpstudy_pro\Extensions\php\php5.6.9nts\php.exe是php的路径。
N:\phpStudy\WWW\www.367edu.com\services\class_task.php是被执行文件。
其实我们在PHP文件中的第56行有这一句:
file_put_contents('a88.log',date('Y-m-d H:i:s',time()).'执行了一次定时任务。'.PHP_EOL,FILE_APPEND);
我们定义a88.log在N盘下。
现在我们只需要把class_task.bat文件定时执行起来即可。因为class_task.bat最终执行的我们定义好的php文件。
步骤:
首先cmd下输入:control打开 控制面板\系统和安全\管理工具
进入后点创建任务
然后按下面的指示执行,选中。
然后切换触发器:
扫描二维码关注公众号,回复:
9006386 查看本文章
然后切换操作:程序或脚本里面填入bat文件的路径即可
点击保存后按如下方式运行即可:
我们刚开始建立了一个日志文件,看到日志里面已经有记录了:
出现以上信息,即代表window计划任务设置成功,同时我们的php文件里面的逻辑也会被执行。think you!