Reputation: 8836
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
Upvotes: 1
Views: 1580
Reputation: 2664
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.
Upvotes: 0
Reputation: 3154
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.
Upvotes: 6
Reputation: 34145
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
Upvotes: 1