autoexec.php:
<?php
ignore_user_abort(); // 即使Client断开(如关掉浏览器),PHP脚本也可以继续执行
set_time_limit(30); // 执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去
$interval=3; // 运行时间间隔(或频率)为3分钟
do{
$url = "http://localhost/testUpdate.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$result = curl_exec($ch);
curl_close($ch);
sleep($interval);// 休眠3分钟
}while(true);
testUpdate.php:
<?php
$fp = fopen("test.html","a+");
fwrite($fp,localtime()."一次\n");
fclose($fp);
?>
test.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<div>
</div>
</body>
</html>
测试运行,如把这几个文件复制到phpEnv的根目录(例如:D:\software\phpEnv\www\localhost),运行autoexec.php。

因为设置超时时间为30秒,因此执行到30秒将自动终止。
访问test.html:

执行结束后的test.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
</body>
</html>
1594476555一次
1594476558一次
1594476561一次
1594476564一次
1594476567一次
1594476570一次
1594476573一次
1594476576一次
1594476579一次
1594476582一次

685

被折叠的 条评论
为什么被折叠?



