I want to make an html file from the rss feed and store it in .html/.htm format and make it static unless I want to change the contents by again running the specific code which gets data from the feed and again changes the contents of that page. But I am tusck in the midway on how to grab data from the feed and make static html page.
Thanks in advance!
Can you post the PHP Code you have for parsing the RSS File?
Lets say these are the steps right now.
1) Retrieve remote RSS file from URL
2) Parse RSS file into PHP Objects
3) Iterate through PHP Objects to generate HTML
4) Echo HTML
In order to cache the generated HTML, you would need something like:1) Check if Local HTML file exists for the URL
2) If file exists, check last modified date of local HTML file
3) If the local file has not expired, echo local HTML file contents
4) If local file does not exist, or is expired, Retrieve remote RSS file from URL
5) Parse RSS file into PHP Objects
6) Iterate through PHP Objects to generate HTML
7) Cache HTML to local file
8) Echo HTML
You can identify each remote RSS URL uniquely by creating a hash of the URL. Then name your local files after this hash.
A hash is a "unique" fixed length string, generated for any arbitrary set of characters.
eg: Create a hash that identified a URL
[PHP]$url = 'http://example.com/rss.xml';
$hash = md5($url);[/PHP]
Now if we can write the url contents to local file
[PHP]$contents = url_get_contents($url);
file_put_contents('cache_dir/'.$hash, $contents);[/PHP]
When the time comes to check if a local version of a url exists, you can do so:
[PHP]if (file_exists('cache_dir/'.md5($url)) { /* exists */ } else { /* not exists */ }[/PHP]
To check when a file was modified use:
[PHP]
filemtime($file);[/PHP]
In our example:
[PHP]$mtime = filemtime('cache_dir/'.md5($url));[/PHP]
So if you want files cached for 1 hour, you can do:
if (($mtime + 3600) > time()) {
/* file is fresh */
}
Note: time() is the seconds from Unix Epoch. 1hr is 3600 secs. $mtime is seconds from unix Epoch when file was last modified.
Now the above will just cache the generated HTML file for 1 hour blindly. It does not check if the actual RSS feed has been modified or not...
A better approach would be to generate database entries for each RSS FEED Item/article.
Or cache each RSS feed Item individually to its own file. Then you can keep track of changing RSS feeds better.