I am using wordpress and would like to display external rss feeds in my theme. So I added the following: function getFeed($feed_url) { $content = file_get_contents($feed_url); $x = new SimpleXmlElement($content); echo "<ul>"; $i = 0; foreach($x->channel->item as $entry) { if($i > 4){$style = "style=\"display: none;\"";} echo " <li $style> <a href='$entry->link' title='$entry->title' target='_blank'>" . $entry->title . "</a> </li>"; $i++; } echo "</ul>"; } PHP: Then I can just add the following code and get the feeds <? getFeed("RSS Feed URL"); ?> PHP: This works like a charm, but with every page load it checks the feeds and causes extra load for the feed supplier. Is there a way to have the feed checked once every 2 hours?
in your function replace the : $content = file_get_contents($feed_url); with $lastcheck = @file_get_contents("timekeeper.txt"); $curtime = time(); if ($curtime - $lastcheck > 2*60*60) { $content = @file_get_contents($feed_url); $contentfile = fopen("contentfile.txt", "w"); fwrite($contentfile, $content); fclose($contentfile); $timefile = fopen("timekeeper.txt", "w"); fwrite($timefile, $curtime); fclose($timefile); } else { $content = @file_get_contents("contentfile.txt"); } this will check the timekeeper.txt and see when the last check was done if it was earlier than 2 hours it will fetch the remote page again and write the content to your contentfile.txt, it will also change the value of last fetch time in your timekeeper.txt to the current time. if it was less than 2 hours ago - it will bring out the content from your local contentfile.txt instead of fetching the remote page again the @ i'm using means "shut up" .. it will prevent PHP from showing errors just in case it is unable to do that particular thing. In this case, it will shut up the PHP error the very first time when you run the script and it tries to open a non-existing timekeeper.txt file. Also, it will shut up PHP from bringing errors if for any reason (network errors) it is unable to fetch data from the remote location.
the fopen function with "w" attribute will create them if your server's settings are right. if they are not created by script, try the followings 1. create the files yourself in the same location as the php script. run your script again, open those files to see if their content is updated by the script ... if the contents are not updated by the script, try the following: 2. chmod the files to 755 or 777 (try 755 first) if it still doesnt help and the script still can't update the content of those files try the following: 3. chmod the containing folder to 755 or 777 (try 755 first) if it doesnt help, try the following: 4. open a ticket with your hosting and tell them to update the configurations of the server so that PHP is able to write to files on local disc. good luuck
Thank you so much. It works, however the files remain empty. But this can also be because there are no feed updates (it is midnight here). I will check them again during the day and keep you posted.
If I do not create the files and set the on 777, then I get a error message that they are not writable. However, when set to 777 everything works, but the files do remain empty. It all works just like the old simple code. With the new code feed is still check with every refresh.
I thank you for the code and your time. After monitoring it for 2 days, your code actually increased load on my own server. Next to the feed that loads everytime, the 2 .txt files keep on loading. So I am going back to the old code. Does anyone else have an idea or solution to have the feed checked once every 2 hours?