大部分情況下我們更新處理數(shù)據(jù)都是在觸發(fā)事件的時候去執(zhí)行它,但是有的時候我們需要無人值守的情況下,定時的去執(zhí)行一些任務(wù),就想鬧鐘一樣,由程序去執(zhí)行任務(wù)。
下面實現(xiàn)定時執(zhí)行的代碼:
定時任務(wù)輔助類
Class TimingAuxiliary
class TimingAuxiliary {
public $dir = '/Timing'; //定時執(zhí)行器所在的文件目錄
public $url; //要執(zhí)行的鏈接地址
public $startFileName; //開關(guān)文件路徑
public $interval; //間隔時間
public $protocol; //鏈接協(xié)議
public function __construct($url, $startFileName, $interval, $protocol = 'http://', $dir = '') {
//驗證值
if (!$url || !$startFileName || !$interval) {
return '請指定完整的數(shù)據(jù)';
}
$this->url = $url;
$this->startFileName = $startFileName;
$this->interval = $interval;
$this->protocol = $protocol;
$this->dir = $dir ? $dir : $this->dir;
}
/**
* 訪問執(zhí)行
* @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 = ''; //需要定時執(zhí)行的地址
/**
* @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;
}
/**
* 執(zhí)行調(diào)用
* @return string
*/
public function run() {
//判斷是否指定了url
if (!$this->url) {
return 'url必須指定';
}
$stopfile = fopen($this->startFileName, "a"); //初始生成文件
$result = "地址:" . $this->url . "啟動成功|占內(nèi)存:" . memory_get_usage() . "\r\n";
fwrite($stopfile, $result);
fclose($stopfile);
ignore_user_abort(); //即使Client斷開(如關(guān)掉瀏覽器),PHP腳本也可以繼續(xù)執(zhí)行.
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
/**
* 中間調(diào)用器
*/
header("Content-Type:text/html;charset=utf-8");
error_reporting(E_ALL ^ E_NOTICE);
include 'Timing.class.php';
//執(zhí)行任務(wù)
$url = urldecode($_GET['url']);
$file = urldecode($_GET['file']);
$interval = $_GET['interval'];
$Timing = new Timing($url, $file, $interval);
$Timing->run();
這里是調(diào)用定時器,和處理的事件,這里只是向test.txt 寫入文字,最好是寫錄日志,可以查看定時器執(zhí)行的狀況;
public function timerun() {
$curl = "http://" . $_SERVER['SERVER_NAME'] . U('Crons/timecs');
$file = 'log.txt'; //刪除即停止執(zhí)行。此處路徑默認(rèn)為.'/Timing/'下的文件可以下成的實列化中定義
import("Org.Util.TimingAuxiliary");
$re = new \TimingAuxiliary($curl, $file, 2);
$re->run();
}
/**
* 定時執(zhí)行的任務(wù)
*/
public function timecs() {
$time=time();
$fp = fopen('..' . . '/Timing/test.txt', 'a');
$content='這是我要寫入的內(nèi)容';
$time=time();
$re = fwrite($fp, $content . memory_get_usage() . "\r\n");
}
最后只要運行 timerun()然后關(guān)掉就可以了
如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請注明來自http://m.oulysa.com/news/5312.html