943,772 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 8207
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 16th, 2006
0

php loading

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MrScruff is offline Offline
89 posts
since Nov 2004
May 18th, 2006
0

Re: php loading

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
May 19th, 2006
0

Re: php loading

Like the Admin said,
[qoute]
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.
[/quote]

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.

[php]
// 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;

}
[/php]

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
May 19th, 2006
0

Re: php loading

[quotes]
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.
[/quotes]

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:

[html]

<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>
[/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".

[code]
<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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
May 19th, 2006
0

Re: php loading

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
BlazingWolf is offline Offline
32 posts
since Feb 2006
May 20th, 2006
0

Re: php loading

Quote originally posted by BlazingWolf ...
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:

[php]

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();

[/php]
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
May 20th, 2006
0

Re: php loading

Very nice.

I never knew you could do that before. That could come in handy.
Reputation Points: 10
Solved Threads: 1
Light Poster
BlazingWolf is offline Offline
32 posts
since Feb 2006
May 26th, 2006
0

Re: php loading

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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MrScruff is offline Offline
89 posts
since Nov 2004
May 26th, 2006
0

Re: php loading

Hi MrScruff,

Could you post how this work and a bit of code please. I'm really interested in this solution, sounds promising.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
May 27th, 2006
0

Re: php loading

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MrScruff is offline Offline
89 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: simple checkbox form array help!
Next Thread in PHP Forum Timeline: PHP Programmer Needed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC