如何在特定间隔(例如每天一次)中运行PHP脚本? [英] How can I run PHP script in certain interval (e.g. once a day)?

查看:62
本文介绍了如何在特定间隔(例如每天一次)中运行PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a php script that reads one file through http(the file is on other domain). I would like to read this file only once or twice a day, instead of connecting to it every time the website is refreshed. Is there any other way than doing it with cron? I dont want to use cron cause I prefer to setup this behaviour in the script itself .. so it is flexible, so I can use it anywhere without setting up cron every time. thanks

推荐答案

如果您不能或不想使用cron,则只有在访问页面时才可以更新它.您可以缓存HTTP请求的结果,并且仅在缓存的时间超过一天或您选择的任何间隔时才在页面加载时对其进行更新.

If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.

这篇关于如何在特定间隔(例如每天一次)中运行PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆