Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting entire response from popen

Hi I'm running a process with popen;-

$handle = popen('python scriptos.py', "r");
while (!feof($handle)) {
    $data = fgets($handle);
    echo "> ".$data;
}

And I'm only getting 3 lines from a process that returns 5 lines. I run this exact command in CLi and I will get more response. It's as if it stops reading early (it can take time to complete and updates the next 2 lines whilst working, it's a progress indicator).

Am I doing anything wrong? Is proc_open more suitable (i've started seeing if I can try that).

like image 509
waxical Avatar asked Feb 23 '23 02:02

waxical


1 Answers

The two missing lines are probably being written to STDERR, and popen() only returns a pointer for STDOUT.

You can either get a pointer for STDERR using proc_open(), or change the popen() line to

$handle = popen('python scriptos.py 2>&1', "r");

to redirect STDERR to STDOUT, so they are included in your output.

like image 128
DaveRandom Avatar answered Mar 07 '23 11:03

DaveRandom