Hi, I'm trying to use Magpie RSS to parse my SQL feed URI's into PHP. It works well but always takes forever to load initially. Anybody else dealt with this problem and resolved it successfully?
Yeah, cache the output using php and load that into your file. Set up a cron job to cache the output every 15 minutes or whatever interval you feel is best.
Ok. This is the bit I don't understand. I've done this but I'm not sure I've done it as I should. I have a stripped down copy of the php file that evokes this RSS feed and that is what I've set up as the file to execute via the cron but it doesn't seem to have any effect. It still takes nearly 1 minute to load the page???
you need to look into two functions, fread and fwrite. You need to read the php file that pulls the RSS, and write it to an HTML file. Then include the HTML file in the main file.
Here's the long answer. This is a simple script to cache a page. <?php function file_get_contents($file) { return implode("\n", file($file)); } function cache_script() { //Set your URL of the file you need cached here: $URL = ""; //get file contents $dynFile = file_get_contents($URL); //next make sure that the RSS feed is retrieved //set staticFile to a location on your web server (/public_html/cache.php). $staticFile = ''; $findme = 'Unable to open RSS Feed'; $pos = strpos($staticfile, $findme); if(!$i) { $i=0; } //If everything went fine, write the file. // Note the use of ===. Simply == would not work as expected if ($pos === false) { $handle = fopen($staticFile, 'w'); fwrite($handle, $dynFile); fclose($handle); echo "Success"; } //if not, repeat up to 5 times, do not set this too high because if the feed is //down, this will just repeat over and over. elseif ($i<5) { $i++; echo "The RSS feed was not found, running again."; cache_script(); } } cache_script(); ?> PHP: Set this up on a cron job to run every so often. Then include the cached file in your page. <?php include('cache.php'); ?>
Okay. I'm obviously still doing something wrong as I'm now getting this error - Cannot redeclare file_get_contents() in ...on line 5
Here's the original code that doesn't seem to be caching real well. <?PHP define('MAX_ITEMS', 100); require_once('magpies/rss_fetch.inc'); $query = 'SELECT query;// Undefined here but defined in real code $result=mysql_query($query); $num=mysql_numrows($result); $urls = Array(); for ($n = 0; $n < $num-1; $n++) { $blogRSS=mysql_result($result,$n,"blogRSS"); $urls[]=$blogRSS; } $items = array(); // loop thru all urls & merge feeds into master array foreach ( $urls as $url ) { $rss = fetch_rss($url); if (!$rss) continue; $items = array_merge($items, $rss->items); } // sort all items in array by date usort($items, 'date_cmp'); if (count($items) > MAX_ITEMS) $items = array_slice($items,0,MAX_ITEMS); // generate ouput array $out = array(); foreach ($items as $item) { $href = $item['link']; $title = $item['title']; $desc = strip_tags(summary($item['description'],35),''); $pubdate = date('D, j M Y',strtotime($item['pubdate'])); $out[] = '<div id="repost"><h3>'.$title.'</h3><h4 style="color:#ece8de;line-height:0.8px; padding-left:5px">'.$pubdate.'</h4><p>'.$desc.'</p><a href="express_pick.php?action=display&id='.$href.'&nme='.$title.'" " title="'.$title.'">[Read more]</a></div><br /> '; } echo ($out) /* ? "<ul>\n".join("\n",$out)."\n</ul>"*/ ? join("\n",$out) : ''; // sorts feed array based on published date (used with usort) // ------------------------------------------------------------- function date_cmp($a, $b) { $atime = (empty($a['date_timestamp'])) ? strtotime($a['dc']['date']) : $a['date_timestamp']; $btime = (empty($b['date_timestamp'])) ? strtotime($b['dc']['date']) : $b['date_timestamp']; if ($atime == $btime) return 0; return ($atime > $btime) ? -1 : 1; } // Limits the length of the description item function summary($content, $limit){ $content = explode(' ',$content); for($i=0; $i<$limit; $i++){ $summary[$i] = $content[$i]; } $summary = implode(' ', $summary).'..'; return $summary; } ?> Code (markup): I put some timers in there to see what was happening and once it got to the point of creating the master array (as shown below) it would take between 4-5 minutes to output. However, once this had happened I could run the code again and it would take less than 1 second. If I left the page and came back about 10 mins later it would again take 4-5 minutes to produce the results. / loop thru all urls & merge feeds into master array foreach ( $urls as $url ) { $rss = fetch_rss($url); if (!$rss) continue; $items = array_merge($items, $rss->items); } Code (markup): I basically ran the same script on another page and had this page automatically served by a cron set for 15 min intervals - but it made no difference. Any ideas?