Not getting entire response from popen

守給你的承諾、 提交于 2019-12-10 16:36:20

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/8523305/not-getting-entire-response-from-popen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!