简体   繁体   中英

Forcing PHP Desktop to stream php responses

The issue I am having I am am needing to steam output when I echo something in a loop. Now typically I would put something such as

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);

ob_end_flush();
while (@ob_end_flush());

ini_set('implicit_flush', true);
ob_implicit_flush(true);

header("Content-type: text/html");
header('Cache-Control: no-cache');

at the top of the page and then call

echo 'Something to print out here';
ob_flush();
flush();

However this is not working at all. I get no errors generated or shown it just doesn't output immediately as desired without buffering. It seems to have no effect at all. I have also tried altering the php.ini file. This also has no effect. I have tried this on 2 different versions of PHP Desktop. I have tried it on PHP Desktop 47.0 Chrome which uses PHP 5.4 and the lastest that just came out PHP Desktop 57.0 which utilizes PHP 7. Any insight would be greatly appreciated.

UPDATE I received a response back from the developer of php desktop and he didn't know why it wasnt working and suggested that Mongoose web server which php desktop utlizes may not support this. Would anyone have more experience with Mongoose than I? I have never really used it other than when using php desktop

The trick to make output buffer flush work in Mongoose is to output a total of 8192 characters before calling ob_flush / flush. Example code below, read php comments and html comments for more details.

<?php

error_reporting(-1);

ini_set('zlib.output_compression', 0);
ini_set('output_buffering', 0);
ini_set('implicit_flush', 1);

// This buffer length value is copied from "mongoose.c" file.
// If you would like to reduce buffer size then modify "mongoose.c"
// file and rebuild phpdesktop from sources.
define("MG_BUF_LEN", 8192);

function fprint($s)
{
    $args = func_get_args();
    call_user_func_array('printf', $args);
    print(str_repeat(" ", MG_BUF_LEN));
    @ob_flush();
    flush();
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");

?>

<style type="text/css">@import url("style.css");</style>
<a href="index.php">Go back to index</a>
| <a href="<?php echo $_SERVER["REQUEST_URI"];?>">Refresh</a>

<title>Output buffer flush</title>
<h1>Output buffer flush</h1>

<p>
    This example forces flush of output buffer repeatedly.
</p>
<p>
    This technique works differently with Mongoose web server.
    Mongoose forces to always read 8192 characters (MG_BUF_LEN)
    before sending output. The solution is to output 8192 spaces
    before each ob_flush / flush calls. Spaces are ignored in html,
    so this output is not visible. It should really be 8192
    characters minus characters previously outputted for it to
    work always correctly. In this simple examples this is not
    required.
</p>

<?php

fprint("\r\n\r\n");
sleep(1);
fprint("Slept for 1 second<br>");
sleep(2);
fprint("Slept for 2 seconds<br>");
sleep(3);
fprint("Slept for 3 seconds<br>");
fprint("Done.")

?>

I've commited the "ob-flush.php" example to phpdesktop:

https://github.com/cztomczak/phpdesktop/blob/2a800fecd8830e4f1e6c4054e74e8d03b4900847/phpdesktop-chrome57/www/ob-flush.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM