大部分情况下我们更新措置数据都是在触发事件的时辰去实行它,然则有的时辰我们需求无人值守的情况下,守时的去实行一些使命,就想闹钟一样,由措施去实行使命。
下面完成守时实行的代码:
守时使命辅佐类
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");