php - 缓存来自远程 URL 的 XML 提要

标签 php xml caching

我正在使用远程 xml 提要,我不想每次都点击它。这是我到目前为止的代码:

$feed = simplexml_load_file('http://remoteserviceurlhere');
if ($feed){
  $feed->asXML('feed.xml');
}
elseif (file_exists('feed.xml')){
    $feed = simplexml_load_file('feed.xml');
}else{
    die('No available feed');
}

我想做的是让我的脚本每小时访问一次远程服务,并将该数据缓存到 feed.xml 文件中。

最佳答案

这是一个简单的解决方案:

检查上次修改本地 feed.xml 文件的时间。如果当前时间戳与 filemtime 时间戳之间的差异大于 3600 秒,则更新文件:

$feed_updated = filemtime('feed.xml');
$current_time = time();

if($current_time - $feed_updated >= 3600) {

         // Your sample code here...

} else {

       // use cached feed...
}

关于php - 缓存来自远程 URL 的 XML 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4371582/

相关文章:

php - 如何过滤波斯语字符的输入?

php - 在 Laravel 中 artisan config :cache actually cache the config as specified in the cache. php 设置?

linux - 在 Intranet 服务器上缓存资源而不是 CDN 以获得更快的网站?

php - iOS应用程序需要POST到PHP脚本,然后解析返回的XML

ios - AFNetworking - 请求和图像的缓存时间如何工作?

php - 如何在Yii中实现表格行拖放排序?

php - mysql查询从多个中选择,按日期排序

php - Mysql速度: table with many columns or 2 tables using a join

java - 从 xsl :fo 生成 html xslt

c# - LINQ TO XML,如何用新值替换值 c#