gpt4 book ai didi

php - 如何以一定的时间间隔运行 PHP 脚本(例如每天一次)?

转载 作者:可可西里 更新时间:2023-10-31 22:40:57 27 4
gpt4 key购买 nike

我有一个 php 脚本,它通过 http 读取一个文件(该文件在其他域上)。我想每天只阅读一次或两次这个文件,而不是每次刷新网站时都连接到它。除了使用 cron 之外还有其他方法吗?我不想使用 cron,因为我更喜欢在脚本本身中设置此行为......所以它很灵活,所以我可以在任何地方使用它而无需每次都设置 cron。谢谢

最佳答案

我过去在无法访问 cron 时做过这种事情:

$lastRunLog = '/path/to/lastrun.log';
if (file_exists($lastRunLog)) {
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 86400) {
//its been more than a day so run our external file
$cron = file_get_contents('http://example.com/external/file.php');

//update lastrun.log with current time
file_put_contents($lastRunLog, time());
}
}

关于php - 如何以一定的时间间隔运行 PHP 脚本(例如每天一次)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/865381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com