I have a php script which is something like below.
script.php
<?php
foreach($available as $loop)
{
echo file_get_contents($loop);
}
?>
basically loop runs like 100 to 200.When I run domain.com/script.php on browser..output is displayed only after all the loop is completed.But I want the page to update the output in realtime.
Is it posible by setting some kinda of header in php script or via htacces ?
You can do this with some servers using the flush() function.
You may be better off using JavaScript and AJAX to fetch and display progress of your long-running task, though.
Related
I'm using ajax to run a simple PHP script which echos numbers. To test if ajax would display each echo as it happens or if it waits for the whole script to finish before displaying anything.
<?php
echo '1';
sleep(5);
echo '2';
sleep(5);
echo '3';
sleep(5);
echo '4';
sleep(5);
echo '5';
?>
This has shown me that it does wait for the whole script to finish before displaying the numbers.
I would like to know if it is possible to output each echo as it happens, similar to how you would see it in shell?
No that's impossible as the PHP script must run and return it's output before it is supplied to the AJAX request.
No it is not possible to wait for fractial result. But you can use websockets to achieve this behaviour.
For example you can use php library Ratchet or use javascript (or other language) as server.
There are many different ways, techniques and software on how to implement what you need.
Here is a basic and easy example for you: https://github.com/panique/php-long-polling
It is based on a long polling technique.
But I would suggest you to use a centrifu-go with sockJs library for a large projects.
My function have for loop and once loop it's output percent of that loop. It's look like this:
function checkisset($v){
foreach ($variable as $key=>$value)
if($v==$value) echo $key/count($variable);
}
and in client i want to show process of loop.
Ex: count($varibale)=10; First time in loop it will be 10%; and when 100% will run success function.
How can i do it??
Tks everyone.
To update your webpage dynamically, you have to establish communication between the client and the server, and send updates.
One way to send updates is "Server sent events". Another is "forever frame", where the page opens javascript link to the server and the server sends javascript statements over time.
In either case, you loop with updates will be a separate resource from the main page that you intend to update.
I am trying to display some info from a ajax call using firephp and firebug.I am able to display someinfo when the ajax call is done. Is there a way to display while it is executing? For example my code below id inside a foreach loop.
foreach ($this->PreQualcalls as $value) {
ob_start();
if(isset($array ['env:Envelope'] ['env:Body']['n1:preQualResponse1207'])){
$this->setKeyNamePreQualResponse("n1:preQualResponse1207");
$this->setKeyNameServiceProducts("n1:ServiceProduct1207");
$this->setKeyNameModemProducts("n1:ModemProduct1207");
$this->firephp->log("entered the 1207 call");
}else{
$this->setKeyNamePreQualResponse("n1:preQual1308Response");
$this->setKeyNameServiceProducts("n1:ServiceProduct1308");
$this->setKeyNameModemProducts("n1:ModemProduct1308");
$this->firephp->log("entered the 1308 call");
}
ob_flush();
$this->firephp->log($this->getKeyNamePreQualResponse(),"Response type");
}
i want to be able to display getKeyNamePreQualResponse() in the console as it is being going through the loop.
Is it possible?
thank you.
FirePHP is designed to collect log information while the PHP script executes and then sends it in special headers along with the page response. It will not show logging calls in Firebug in realtime.
To debug your code using logging, place more logging calls into the loop. e.g.
$this->firephp->log("value", $value);
You have a loop that assigns a new value to $value on each iteration but don't use it in any of the statements in the loop. That does not seem right.
If you need realtime debugging I suggest using xDebug or other realtime logging tool.
I'm have a long-running perl script that outputs the percentage complete. How do I show the completion status real-time on a php page?
Example:
perl:
my $i = 0;
for $i (1 .. 6) {
print "$i\n";
sleep 1;
}
print "script end\n";
exit;
php:
echo passthru('perl testprint.pl');
This works to display the output, but not real-time.
Modify your perl script to pipe the output to a flat file. Use a php script to parse this file and output in whatever format you want.
You could call the php script from your html page using ajax, or if you save the perl script in the server's CGI directory you can call it directly without the need for the extra php file.
Lastly, create a nifty progress bar and profit.
For that you need to utilize AJAX.
I would have a PHP script that looks at the status, and then have AJAX update the status for the user to see.
I have a foreach loop that calls a function to set values to an array. Sometimes it takes hours to complete depending on how many times it has to run thru the function to complete.
What I would like to have is a progress bar or at least a 1/1000 completed type progress indicator.
Is this possible? If so how could I implement this into my code? Would it be in the function or in the foreach loop? Been researching and found some examples using for and $i++ but I am not really sure how to implement that since I am already using a foreach loop.
Thanks much.
function scrape_amazon($links) {
//my code runs here to set all values in $ret array.
}
foreach($links as $link) {
$ret = scrape_amazon($link);
}
PHP probably isn't really the right tool for this task, however what you could do is:
Launch the slow code as a background process, and output progress to a file.
Have a PHP script that polls that file for progress information (either by page refresh or AJAX)
Launching the background process can be done in several ways, including:
Launch via cron every 60 seconds, and poll for new jobs spooled in some readable area
Launch via a fork/exec mechanism from a web page
Launch as a daemon at system startup
It will take some effort to avoid problems with multiple executions and/or overlap.
I use this, which well, not an ajax, do only flushing, but not so ugly.
I place an image
<img src='progress.gif' height=18 width=0 name=probar>
Then set on every event done on server a echo a line, then flush:
echo "<script language='JavaScript'>\ndocument.probar.width=".(($sys["probar_width"]/$task_all)*$task_i).";\n</script>\n";
flush();
If your server (eg. apache) use caching (eg. gzip is enabled) it won't work well.