简体   繁体   English

PHP exec()输出缩短

[英]PHP exec() Output Cut Short

I have a PHP file that runs a node script using exec() to gather the output, like so: 我有一个PHP文件,该文件使用exec()运行节点脚本以收集输出,如下所示:

$test = exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/");
echo $test;

It outputs a JSON string of data tied to the website in the --url paramater. 它在--url参数中输出绑定到网站的JSON数据字符串。 It works great, but sometimes the output string is cut short. 效果很好,但有时输出字符串会被缩短。

When I run the command in the exec() script directly, I get the full output, as expected. 当我直接在exec()脚本中运行命令时,我得到了预期的完整输出。

Why would this be? 为什么会这样呢? I've also tried running shell_exec() instead, but the same things happens with the output being cut short. 我也尝试过运行shell_exec(),但是同样的事情也会发生,因为输出被缩短了。

Is there a setting in php.ini or somewhere else to increase the size of output strings? php.ini或其他地方是否有设置来增加输出字符串的大小?

It appears the only way to get this working is by passing exec() to a temp file, like this: 看来唯一可行的方法是将exec()传递给临时文件,如下所示:

exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/ > /home/user/www/uploads/json.txt");
$json = file_get_contents('/home/user/www/uploads/json.txt');
echo $json;

I would prefer to have the direct output and tried increasing output_buffering in php.ini with no change (output still gets cut off). 我更喜欢直接输出并尝试不加更改地增加php.ini中的output_buffering (输出仍然被切断)。

Definitely open to other ideas to avoid the temp file, but could also live with this and just unlink() the file on each run. 绝对可以避免使用临时文件,但也可以使用此文件,并且每次运行都只需unlink()

exec() only returns the last line of the output of the command you pass to it. exec()仅返回传递给它的命令输出的最后一行 Per the section marked Return Value of the following documentation: 根据以下文档的标为“返回值”的部分:

The last line from the result of the command. 命令结果的最后一行。 If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. 如果您需要执行命令并直接将命令中的所有数据传回而不会受到任何干扰,请使用passthru()函数。

To get the output of the executed command, be sure to set and use the output parameter. 要获取已执行命令的输出,请确保设置并使用输出参数。

To do what you are trying to do, you need to pass the function an array to store the output, like so: 要执行您想做的事情,您需要向函数传递一个数组以存储输出,如下所示:

exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/", $output);
echo implode("\n", $output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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