大部分情况下我们更新处理数据都是在触发事件的时候去执行它,但是有的时候我们需要无人值守的情况下,定时的去执行一些任务,就想闹钟一样,由程序去执行任务。
下面实现定时执行的代码:
定时任务辅助类
Class TimingAuxiliary
class TimingAuxiliary {
public $dir = '/Timing'; //定时执行器所在的文件目录
public $url; //要执行的链接地址
public $startFileName; //开关文件路径
public $interval; //间隔时间
public $protocol; //链接协议
public function __construct($url, $startFileName, $interval, $protocol = 'http://', $dir = '') {
//验证值
if (!$url || !$startFileName || !$interval) {
return '请指定完整的数据';
}
$this->url = $url;
$this->startFileName = $startFileName;
$this->interval = $interval;
$this->protocol = $protocol;
$this->dir = $dir ? $dir : $this->dir;
}
/**
* 访问执行
* @return mixed
*/
public function run() {
$url = $this->protocol . $_SERVER['SERVER_NAME'] . . $this->dir . "/TimingRun.php?url=" . urlencode($this->url) . '&file=' . urlencode($this->startFileName) . '&interval=' . $this->interval;
$hander = curl_init();
curl_setopt($hander, CURLOPT_URL, $url);
curl_setopt($hander, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($hander, CURLOPT_HEADER, 0);
curl_setopt($hander, CURLOPT_TIMEOUT, 20);
$result = curl_exec($hander);
return $result;
}
}
* 定时处理类 Timing.class.php
class Timing {
public $interval = 1; //间隔时间
public $startFileName = "timing_stop.txt"; //停止文件,填写其完整路径
public $url = ''; //需要定时执行的地址
/**
* @param $url
* @param string $startFileName
* @param int $interval
*/
public function __construct($url, $startFileName = '', $interval = 0) {
$this->url = $url;
$this->startFileName = $startFileName ? $startFileName : $this->startFileName;
$this->interval = $interval ? $interval : $this->interval;
}
/**
* 执行调用
* @return string
*/
public function run() {
//判断是否指定了url
if (!$this->url) {
return 'url必须指定';
}
$stopfile = fopen($this->startFileName, "a"); //初始生成文件
$result = "地址:" . $this->url . "启动成功|占内存:" . memory_get_usage() . "\r\n";
fwrite($stopfile, $result);
fclose($stopfile);
ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
set_time_limit(0);
$interval = $this->interval;
$start = file_exists($this->startFileName);
do {
//curl比file_get_contents性能要高
$hander = curl_init();
curl_setopt($hander, CURLOPT_URL, $this->url);
curl_setopt($hander, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($hander, CURLOPT_HEADER, 0);
curl_setopt($hander, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
curl_setopt($hander, CURLOPT_TIMEOUT, 30);
curl_exec($hander);
sleep($interval);
$start = file_exists($this->startFileName);
flush(); //刷新输出缓冲
} while ($start);
}
}
TimingRun.class.php
/**
* 中间调用器
*/
header("Content-Type:text/html;charset=utf-8");
error_reporting(E_ALL ^ E_NOTICE);
include 'Timing.class.php';
//执行任务
$url = urldecode($_GET['url']);
$file = urldecode($_GET['file']);
$interval = $_GET['interval'];
$Timing = new Timing($url, $file, $interval);
$Timing->run();
这里是调用定时器,和处理的事件,这里只是向test.txt 写入文字,最好是写录日志,可以查看定时器执行的状况;
public function timerun() {
$curl = "http://" . $_SERVER['SERVER_NAME'] . U('Crons/timecs');
$file = 'log.txt'; //删除即停止执行。此处路径默认为.'/Timing/'下的文件可以下成的实列化中定义
import("Org.Util.TimingAuxiliary");
$re = new \TimingAuxiliary($curl, $file, 2);
$re->run();
}
/**
* 定时执行的任务
*/
public function timecs() {
$time=time();
$fp = fopen('..' . . '/Timing/test.txt', 'a');
$content='这是我要写入的内容';
$time=time();
$re = fwrite($fp, $content . memory_get_usage() . "\r\n");
}
最后只要运行 timerun()然后关掉就可以了