Reputation: 2669
My script consists of two parts. The first part generates HTML page. The second part loads a resourse from the net and saves it into a file. Loading is a long operation and can take up to 10 seconds. The first and the second parts are completely indepenent (loading has no impact on the look of the page).
At the end of the first part I call flush(), that makes page displayed instantly.
However, embedded SWF movie is not displayed at this moment.
SWF is the key element of the page and it must be displayed as fast as possible. It is embedded in the page with SwfObject library. And the problem is that SwfObject starts to open SWF on "onLoad" event of the page.
But "onLoad" event comes at the end of the second part. That is "onLoad" can occure in 10 seconds. So in spite flush() call the user has to wait for 10 seconds in vain.
Is it possible to solve the task on PHP side? Is it possible to run "loading" task (the second part) in a separate process? Is it possible to tell the browser before the second part starts, that the page is reay and you can broadcast the "onLoad" event.
Thanks for reading this very long story.
Upvotes: 1
Views: 787
Reputation: 6671
I suggest splitting your code into two separate PHP files:
The first file generates the HTML page containing the SWF.
The second file fetches the resource and saves it.
The trick is that in the onLoad event of the first page you run a small snippet of JavaScript which requests the URL of the second page.
The code in the first page would look something like this:
...
<body onload="makeRequest('page2.php')">
...
where makeRequest is a function something like the one here: http://www.captain.at/howto-ajax-form-post-get.php
This AJAX Tutorial make be helpful for understanding that.
Of course, if these things are truly independent you should look at running the second one in a separate process or thread. pcntl-fork may help here.
Upvotes: 2
Reputation: 39606
One approach is to initiate the second stage via a one-way webservice request from the first stage. Assuming that you're using PHP 5, take a look at the SOAP support.
If the client is not loading after you've called flush(), then there are two possibilities: first, the web-server may be buffering output and it's not getting to the client; second, you haven't produced a complete page at the time you call flush(). The latter problem should be easy to resolve.
Upvotes: 0
Reputation: 37822
I'm not sure, but what if you put this SWF movie inside an IFRAME?
Upvotes: 1
Reputation: 2685
Instead of "onload", can you add a script block in the end of the page to initialize your SWF movie?
Upvotes: 1
Reputation: 70414
PHP doesn't support threading so you would have to write a script that will do the second part and run it using exec()
method. But that's probably not the best solution as well.
What you probably can do is forget about move the second part that takes long time to separate script or controller action if you are using MVC controllers. Then just run the first part and your page will load quickly. Then you can add a AJAX to call that new script/action to run the second part of your algorithm after your page has loaded.
Upvotes: 2