Is it possible to refresh the screen and wait for user input at the same time as PHP?

I want to write a small management tool to control my server processes, now my problem is, how can I wait for user input and simultaneously update the screen with current statistics? Is this possible using PHP-CLI or are there any tricks for this that I am missing right now?

I studied the triggers and ncurses of the PECL extension, but both do not seem to fit my needs.

+7
source share
4 answers

Go for libevent http://www.php.net/manual/en/book.libevent.php

You can start your main loop while listening to the console with the code like this:

<?php // you need libevent, installable via PEAR $forever=true; $base=event_base_new(); $console=event_buffer_new(STDIN,"process_console"); event_buffer_base_set($console,$base); event_buffer_enable($console,EV_READ); while ($forever) { event_base_loop($base,EVLOOP_NONBLOCK); // Non blocking poll to console listener //Do your video update process } event_base_free($base); //Cleanup function process_console($buffer,$id) { global $base; global $forever; $message=''; while ($read = event_buffer_read($buffer, 256)) { $message.=$read; } $message=trim($message); print("[$message]\n"); if ($message=="quit") { event_base_loopexit($base); $forever=false; } else { //whatever..... } } 
+1
source

I do not think you can do this using the PHP CLI. As I know, when interpreting a script with PHP you can only view the final output.

0
source

I think you want ncurses. If you can convert a simple C-code example here that you should have with a PHP wrapper, you will have your “boot” option for solving your problem.

Be sure to register your code somewhere! :)

0
source

My advice would be to try to avoid any solutions that talk about avoiding the processes that were started during the exit from PHP. Here is a simple example of how to do this using jQuery:

 window.setInterval(checkstat, 10000); //10 second interval function checkstat() { //Change a div with id stat to show updating (don't need this but it nice) $('#stat').html('Updating...'); $.get('/getmystats.php?option=blah', function(data) { //Update the results when the data is returned. $('#stat').html(data); }); } 

If you need to update more than one area on your page, you can make one call, but return JSON or XML, and then fill in the bits as needed.

-one
source

All Articles