Hello. I have a php webpage which extracts RSS feeds and displays them. The problem is it takes a while for the page to load - I expect that its looking up the latest feed data. However I was wondering if any of you knew a way I could get the page to load up the HTML first and then display the feeds. That way users aren't just left with a blank page for 5 or 6 seconds.
The page I'm talking about is here...

http://www.blackdrop.org/dashboard/music.php

Thanks for any help.

Steve!

Have you considered using a third party PHP parser? I know this isn't a solution to your problem, but I use MagpieRSS PHP parser and I absolutely love it.

Like the Admin said,

Have you considered using a third party PHP parser? I know this isn't a solution to your problem, but I use MagpieRSS PHP parser and I absolutely love it.

you could use a third party PHP, RSS parser.
How this solves your loading problem is that most these third party RSS or XML parsers have an inbuilt caching system, that caches RSS feeds.H
(This is required in the RSS Specs).

When retrieving RSS feeds, many sites will not allow you to request a feed more than once within an hour.

If you want to cache RSS feeds yourself, you can use something like this file caching function I found on the web, when you retrieve the rss file.

// cache a (http) fopen - with cache-timeout.
function cached_fopen($file, $file_mode, $timeout_seconds = 600, $cache_path = "/tmp"){

  $debug=false;

  clearstatcache();
  $cache_filename=$cache_path . "/" . urlencode($file) .".cached";

  if ($debug) { print "local_cache creation_time =" . @filemtime($cache_filename) . " actual time = " . time() . " timeout = " . $timeout_seconds ."<p>";}

  if ( ( @file_exists($cache_filename ) and ( ( @filemtime($cache_filename) + $timeout_seconds) > ( time() ) ) ) ){
   // ok, file is already cached and young enough
     if ($debug) { print "using cached file ($cache_filename) <p>";}
   }
  else
  {
   if ($debug) { print "cacheing file ($file) to local ($cache_filename)<p>";}

   // cache file from net to local
   $f = fopen($file,"r");
   $f2 = fopen($cache_filename,"w+");
   while ($r=fread($f,8192) ) {
     fwrite($f2,$r);
   }
   fclose($f2);
   fclose($f);
  }

  // ok, point to (fresh) cached file
  $handle = fopen($cache_filename, $file_mode);
  return $handle;

}

Whta the funciton does is first check if there is a local version of the file available and still "fresh", then gets it from the remote server if otherwise.
So for your RSS files, it will first check if it has a local version of your RSS file saved and within the timeout, otherwise, download the RSS file.

Hope that helps.

Edited 3 Years Ago by Dani: Formatting fixed

However I was wondering if any of you knew a way I could get the page to load up the HTML first and then display the feeds.

You can't really choose which part of the page to load first with PHP.
However you can use client side methods to do this.

Theres a few, heres from simple to a bit harder:

1) Use frames:
Have an iframe which serves up just the RSS html output and embed the iframe where you want on the main page.

2) Use JavaScript's innerHTML function.
You can set up your page so that the RSS data loads right at the bottom of the page.

Something like:

<body>

<div id="rss_position">

</div>

<!--

Your page content here


-->

<div id="rss_data" style="display:none;">

<?php

// retrieve rss data and echo here

?>

</div>

</body>
</html>

This will make the rest of the page load first. The bottom of the page will load last, and it contains the RSS data.
The rss data is loaded within the div with id "rss_data" which has a display:none style set. This prevents the div from displaying on the screen.

Then you use a bit of javascript to take the rss data/html from the hidden div at the bottom of the page, to its position at the div of id "rss_position".

<script type="text/javascript">

window.onload = function() {
  var rss_loader = document.getElementById('rss_data');
  var rss_display = document.getElementById('rss_position');

  rss_display.innerHTML = rss_loader.innerHTML;

}

</script>

The bit of javascript is executed when the document is fully loaded, and thus your rss data is loaded.
This is after you other html is already visible.

3) Use JavaScript Remoting or XMLHTTPRequest

You can use "AJAX", to retrieve the RSS data after the page is loaded.
I'll write up a blog entry on it and post the link here if you're interested.

Hope that helps.

Edited 3 Years Ago by Dani: Formatting fixed

Well when your syndicating from 23 blogs like your page says it just going to take some time.

Since PHP can't multithread it has to do each connection one at a time.

Like digital-ether said toward the end of her post you can use AJAX to load the RSS feeds after the page is loaded.

Although I'm like 90% sure (only digitals confidence in it is making me second guess myself) #2 won't help you because even having the PHP parse last will still prevent the page from displaying before the execution is complete.

Best way to go is AJAX.

Although I'm like 90% sure (only digitals confidence in it is making me second guess myself) #2 won't help you because even having the PHP parse last will still prevent the page from displaying before the execution is complete..

Hey, you're absolutely right!
Normally your script would have to terminate before any output is sent to the browser via HTTP.

However, you can cause your server to flush the php output to HTTP prematurely.
This is done using the flush() function. You can read more on it here: http://www.php.net/flush

If you are also buffering the php output in the script using, ob_start(), then you will have to call ob_flush() before calling flush().

flush() also does not seem to work on Win/Apache. It does on Linux/Apache though.

Also, IE needs to receive 256 bytes via http before rendering content.

Heres how you could do it:

echo 'This is some content I want shown immediately';

// echo a hundred dummy line breaks (\r\n). 
// This causes IE to start rendering received http text
for($i = 0; $i < 100; $i++) {
  echo "\r\n";
}

ob_flush(); // if you have used ob_start() to buffer php 
flush(); // send output buffer (seperate from ob_start() buffer) to http

// now do the rss stuff
echo retrieve_rss_feeds();

Hey thanks for all your replies. :) I just use a redirect page which loads up quick and then people can start using it while they wait for the feeds to load. Thankyou!

Hi MrScruff,

Could you post how this work and a bit of code please. I'm really interested in this solution, sounds promising.

Well basically I made a html page which is exactly the same as my main php page:

http://www.blackdrop.org/dashboard/music.htm
http://www.blackdrop.org/dashboard/music.php

The user loads up the htm file first which loads up really quickly and allows them to use the lyric and download searches while the redirect page works it magic by loading the next page. The htm file has this redirect code in it:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.blackdrop.org/dashboard/music.php">

It seems to work. :)

Well basically I made a html page which is exactly the same as my main php page:

http://www.blackdrop.org/dashboard/music.htm
http://www.blackdrop.org/dashboard/music.php

The user loads up the htm file first which loads up really quickly and allows them to use the lyric and download searches while the redirect page works it magic by loading the next page. The htm file has this redirect code in it:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.blackdrop.org/dashboard/music.php">

It seems to work. :)

Excellent...this is exactly what I'm looking for myself! I'm trying to syndicate from various blogs and my RSS page takes at least 10.5 seconds to load!

This article has been dead for over six months. Start a new discussion instead.