如何每 6 小时保存一个 RSS 提要并将其重命名为当前日期?

发布于 2024-11-19 06:21:09 字数 629 浏览 0 评论 0原文

我在我的网站中显示一些 RSS 提要,并将它们放在数组中。我正在研究如何通过创建前一天的存档系统并将我使用的 RSS 提要保存到文件夹中以供进一步使用来改进脚本。

我所知道的是显示按日期排序的提要帖子。我不知道如何在我拥有脚本的同一文件夹中每隔 6 小时保存一组 RSS 提要。我不介意这项工作是通过 cron 完成还是由我手动完成。

也许解决方案是创建一个包含所有提要的单个提要,该提要将由脚本以日期格式结构(如 11072011.xml)重命名。6

小时后,将有一个必须保存的新提要。因此,要么与前一个合并(我不介意是否有相同的条目),要么系统必须创建一个名为 110720112.xml 的新文件(2 是增量号),其中包含我的提要数组的所有帖子。

如果这是解决方案(而不是组合它们),则第一个 XML 文件应以数字 1 结尾,如 110720111.xml 等,以便进行排序。

我想象的是在代码中包含我使用的所有 rss feed,并创建一个 cron 作业,将它们保存到同一个文件夹中。

我知道如何使用

file_get_contents();file_put_contents(); 但只了解它们之间的一些信息。

感谢您提供的示例、代码、链接、想法

I am displaying some RSS feeds in my site that I have them in an array. I am researching on how to improve the script by creating an archive system for the previous day and save the RSS feeds I use into a folder for further use.

What I know is to show the posts of the feeds sorted by date. What I don't know is how to save an array of RSS feeds every let's say 6 hours in the same folder I have the script. I don't mind if the job is made through cron or manually by me.

Perhaps a solution is to create a single feed with all the feeds combined that will be renamed by the script in a date format structure like 11072011.xml

After 6 hours there will be a new feed that must be saved. So, either will be combined to the previous one (I dont mind if there are same entries) or the system must create a new file called 110720112.xml (2 is the increment number) with all the posts of my feeds array.

If this is the way of solution (and not combining them), the first XML file should have at the end number 1 like 110720111.xml and so on for sorting purposes.

What I imagine is having in the code all the rss feeds I use and create a cron job that will save them into the same folder.

I know how to use

file_get_contents(); and file_put_contents(); but just a bit about the between of them.

Thank you for your examples, codes, links, ideas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乄_柒ぐ汐 2024-11-26 06:21:10

合并 feed 内容可能不值得,因为它可能会破坏 xml 结构。因此,这里有一个快速脚本,可以使用每天增量名称归档到单独的文件中...

$rssContents = file_get_contents('http://url/for/rss');
$arcPath = '/path/to/archive/folder/';
$fileBaseName = date('mdY');
$x = 1;

while (file_exists($arcPath . $fileBaseName . $x . '.xml')) {
    $x++;
}
file_put_contents($arcPath . $fileBaseName . $x . '.xml', $rssContents);

您只需每六个小时将其作为 cron 作业运行一次即可。您还应该添加错误测试,以确保从 rss url 检索内容,并且存档目录存在。您可能还想添加一些内容来删除较旧的文件(可能超过 30 天),以免目录变得混乱。

Combining feed contents may not be worth it as it would likely just ruin the xml structure. So, here's a quick script to archive into separate files using incremental names per day...

$rssContents = file_get_contents('http://url/for/rss');
$arcPath = '/path/to/archive/folder/';
$fileBaseName = date('mdY');
$x = 1;

while (file_exists($arcPath . $fileBaseName . $x . '.xml')) {
    $x++;
}
file_put_contents($arcPath . $fileBaseName . $x . '.xml', $rssContents);

You can simply run it as a cron job every six hours. You should also add error testing to ensure content is retrieved from the rss url, and that the archive directory exists. You may also want to add something to delete older files (maybe +30 days) to keep from cluttering up the directory.

绳情 2024-11-26 06:21:10

您可以尝试通过 Cron 每小时运行一些类似的代码

$rss_feed_contents = file_get_contents('feed_url_goes_here');
$file_name = date("Ymd") . 'xml';
........some stuff here.......
file_put_contents($rss_feed_contents, $file_name);

这个确切的代码行不通,但我希望您明白

You can try to run some code similar this every hour via Cron

$rss_feed_contents = file_get_contents('feed_url_goes_here');
$file_name = date("Ymd") . 'xml';
........some stuff here.......
file_put_contents($rss_feed_contents, $file_name);

This exact code won't work but I hope you get the idea

别闹i 2024-11-26 06:21:10

您可以通过创建 cronjob/计划任务来完成此操作。您应该安排一个脚本每 6 小时运行一次,并且该脚本应该创建您需要的文件。

第二种方法是不使用 cronjob。它可能不会每次都有效,但如果您无法访问 cronjobs/计划任务,那么这是一个好方法。你需要做的是:
每次打开某个页面时,您都应该根据这一小时的规则生成文件名。然后使用 file_exists 检查该文件是否已创建,如果没有,则创建它。如果您的网页每 6 小时至少有 1 位访问者,您将获得出色的结果。
文件名可以根据php日期('H')使用今天的日期+后缀1、2、3或4生成。您必须检查这些间隔:0-6、7-12、13-18 和 19-23,并根据结果 - 使用 1、2、3 或 4 后缀。

希望这有帮助。

You can do this by creating a cronjob / scheduled task. You should schedule a script to run every 6 hours and the script should create the file you need.

The second way to do this is without a cronjob. It might not work each time, but it's a good way if you don't have access to cronjobs / scheduled tasks. What you need to do is:
Every time a certain page is opened you should generate the file name according to your rules for this hour. Then you check using the file_exists if this file has been created and if not - create it. If your webpage has at least 1 visitor each 6 hours you will get brilliant results.
The file name can be generated using today's date + a suffix 1,2,3 or 4 according to the php date('H'). You have to check for those intervals: 0-6, 7-12, 13-18 and 19-23 and according to the results - use the 1,2,3 or 4 suffix.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文