Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo messages while php script still executes

Tags:

php

curl

messages

I have a php script that uses cURL and takes about 10-15 minutes to execute. What it does, it parses about 1000 pages looking for specific matches and throughout the script I have diagnostic messages echo'ed out, like "Going to the next page", "Found a match", "Error loading page" ... The way it works now (and the way that it's normal) is it executes for like 10 minutes and only then spits out all my custom messages. I would like to be able to display those messages as they happen, not when the script is done executing. I was thinking something like AJAX would do it, but am not sure how it would work. Any tips are greatly appreciated. Thanks.

like image 891
pHelics Avatar asked Jan 12 '11 14:01

pHelics


People also ask

Can you echo a script in PHP?

The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console.

What can I use instead of echo in PHP?

The PHP print Statement You can also use the print statement (an alternative to echo ) to display output to the browser. Like echo the print is also a language construct not a real function. So you can also use it without parentheses like: print or print() .

Where does PHP echo go?

php echo directly in the browser. If you directly view backend. php in your browser you will see the echo in the same way as index.

How PHP scripts are executed?

Broadly speaking, the PHP interpreter goes through four stages when executing code: Lexing. Parsing. Compilation.


3 Answers

So, this is a old post but I found a solution for this. As I also have to make the same thing, output when the script is still running. Not any answer from here helped. First of all, I am using Win32 server(production) and XAMPP as local for tests. This example is just a proof of concept and can be modified as you please.

<?php 
ob_implicit_flush(true);
for($i=1; $i<=10; $i++){
echo "$i ...<br>";
for($k = 0; $k < 40000; $k++) echo ' ';
sleep(1);
}
?>

So, we open output buffer as implicit. Then we make a demo loop to count from 1 to 10 and display the values as they are been processed. Second loop will fill in the browsers buffer. And finally to check if everything is working well we make a sleep for 1 second. Otherwise the script will run too fast and we could not know if we achieved the goal. Hope this helps !

like image 159
Bogdan Avatar answered Oct 20 '22 11:10

Bogdan


You could create a staging table.

The PHP script could, instead of echo'ing the message, store them into a database table (possibly memory table for performance).

You could then periodically poll a seperate PHP script using ajax, which would query the table, and return any new messages to the client.

like image 43
Mads Mogenshøj Avatar answered Oct 20 '22 10:10

Mads Mogenshøj


Use flush to immediately send output to the browser, by flushing the output buffer.

echo "foo";
flush();
echo "bar";
flush();
like image 37
karim79 Avatar answered Oct 20 '22 09:10

karim79