PHP-运行shell_exec()不会返回所有输出

Raz*_*riz 6 php terminal shell-exec python-3.x

我正在pdfgrep搜索PDF文档中关键字的所有外观。

现在,我想通过PHP执行此操作,以便可以在我的网站中使用它。

但是,当我运行时:

$output = shell_exec("pdfgrep -i $keyword $file");
$var_dump($output);
Run Code Online (Sandbox Code Playgroud)

$keyword关键字和$file文件在哪里,我没有得到全部输出。

PDF由产品代码,产品名称和产品价格表组成。

通过终端执行命令时,可以看到整行数据:

product code 1    product name with keyword substring    corresponding price
product code 2    product name with keyword substring    corresponding price
product code 3    product name with keyword substring    corresponding price
Run Code Online (Sandbox Code Playgroud)

但是,当我通过PHP运行它时,我得到如下信息:

name with keyword substring with keyword substring product code 1 
product name with keyword substring product name with keyword substring 
corresponding price
Run Code Online (Sandbox Code Playgroud)

它只是不获取所有数据。它并不总是获得产品代码和价格,并且在很多情况下也没有获得整个产品名称。

我通过浏览器查看输出并放入,header('Content-Type: text/plain');但是它只能美化输出,数据仍然不完整。

我试图通过Python3.6运行完全相同的shell脚本,这给了我所需的输出。

现在,我尝试通过PHP运行相同的Python脚本,但仍然得到相同的损坏输出。

我尝试运行一个我知道会返回较短输出的关键字,但是我仍然没有得到所需的整个数据行。

有什么方法可以可靠地获取shell_exec()命令抛出的所有数据吗?

是否有其他替代方法,例如不同的命令或从服务器运行Python脚本(因为Python脚本始终没有任何问题)。

Kri*_*ris 5

我不知道pdfgrep的工作方式,但也许它混入了stdout和stderr?无论哪种方式,都可以使用类似的构造,在该构造中,将输出流捕获到输出缓冲区中,还可以选择将stderr混合到stdout中:

$mixStdErrIntoStdOut = false;

ob_start();
$exitCode = 0;
if ($mixStdErrIntoStdOut) 
{
    system("pdfgrep -i $keyword $file 2>&1", &$exitCode);
} else {
    system("pdfgrep -i $keyword $file", &$exitCode);
}
$output = ob_get_clean();

var_dump($output);
Run Code Online (Sandbox Code Playgroud)