我可以每隔设定的时间读取外部文件吗?
我正在编写一个脚本,它会检查来自外部服务器的更新版本。我在 config.php 文件中使用此代码来检查最新版本。
$data = get_theme_data('http://externalhost.com/style.css');
$latest_version = $data['Version'];
define('LATEST_VERSION', $latest_version);
这很好,我可以获取最新版本(get_theme_data 是 WordPress 函数),但问题是它将在我不想要的每个负载上执行。例如,我也不想只检查提交表单的时间。或者,我正在研究某种方法来缓存结果,或者每隔设定的时间检查版本?这样的事情可能吗?如何实现?
I am writing a script where it checks for an updated version from an external server. I use this code in the config.php file to check for latest version.
$data = get_theme_data('http://externalhost.com/style.css');
$latest_version = $data['Version'];
define('LATEST_VERSION', $latest_version);
This is fine and I can fetch the latest version (get_theme_data is WordPress function) but the problem is that it will be executed on every single load which I do not want. I also do not want to only check when a form is submitted for example. Alternatively I was looking into some sort of method to cache the result or maybe check the version every set amount of hours? Is such thing possible and how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,让你轻松搞定。将您上次检查更新的时间存储在文件中。
如果您愿意,显然可以使其更加安全/高效。
编辑:此功能将每天检查一次更新。
Here, gonna make it easy for you. Store the time you last checked for the update in a file.
You can obviously make this much more secure/efficient if you want to.
Edit: This function will check for update once every day.
像这样的计划任务应该设置为单独的
cron
或at
作业。您仍然可以用 PHP 编写所有内容,只需制作一个从命令行运行并执行更新的脚本即可。查看“man crontab”了解详细信息,和/或检查您的服务器正在运行哪些调度服务。A scheduled task like this should be set up as a separate
cron
orat
job. You can still write everything in PHP, just make a script that runs from the command line and does the updating. Checkout "man crontab" for details, and/or check which scheduling services your server is running.