How to thread filter standard PHP output?

Is it possible to filter PHP standard output data in streaming mode:

standard output ⟶ output filter ⟶ standard output 

I already know about ob_start . But I do not want to process all the output at once, but in a stream, using php_user_filter or something like that.

+4
source share
2 answers

I do not quite understand why this is necessary, but there is no reason not to publish the answer.

You can use the ob_start() and handle partial content. All you have to do is set ob_implicit_flush() right after initialization. Now usually a callback is a simple login function, but you can make it as complex as possible with:

 class ob_callback { function __invoke($part, $end_flag_0x04) { return "+$part"; // or map to $stream->filter($in, $out, &$consumed, $closing) } function __destruct() { /* cleanup */ } } ob_start(new ob_callback, 2); ob_implicit_flush(TRUE); 

I'm not sure what the stream will look like. But I think there is no other way to intercept standard PHP output. Note that an implicit flash will not work in the CLI.

+3
source

If I understand your question correctly, you can use the second argument for ob_start() , $chunk_size for this.

ob_start('my_callback', 1024);

The above example will call my_callback() every time the output causes the buffer to reach or exceed one kilobyte. If you splash out a few kilobytes in separate statements, my_callback() will fire several times. This would not be useful if you would output several kilobytes as a single line, since the maximum my_callback() can be run only once per output.

0
source

Source: https://habr.com/ru/post/1342502/


All Articles