从PHP运行Node.js脚本 - 输出被截断为512个字符

We run node.js CLI script from PHP with Symfony Process.

The script always print whole response as JSON in one line.

The response is somehow truncated on 512 characters.

I only found that xdebug.var_display_max_data => 512 => 512 in php.ini but don't see how this is related.

Adapter > Symfony Process > node script.js

A) Test Node script

  1. from terminal node script $ node user-update.js parameters returns full result in all cases - like 629 chars.
  2. from Symfony Process node script response is truncated to 512 chars.

B) Test Symfony Process

$process = new Process($cmd);
try {
    $process->mustRun();
    $response = $process->getOutput();
} catch (ProcessFailedException $e) {
    $response = $e->getMessage();
}
echo $response;
echo PHP_EOL;
echo strlen($response);
  1. $cmd = 'node user-update.js parameters'; - truncated to 512.
  2. $cmd = 'php -r \'for($i=0; $i<520; $i++){ echo "."; }\''; - does not truncate.
  3. $cmd = 'cat long_one_line.txt'; - print full file. 1650 chars in one line.

C) Try with PHP shell functions

$response = shell_exec($cmd); // response is truncated to 512
system($cmd, $returnVal); // print directly to STDOut, truncated to 512

What could be the cause and solution?

  • node v7.6.0
  • PHP 7.1.2

I suspect your process is ending before the buffer can be read by PHP.

As a work-around you can add something like this:

// The `| cat` at the end of this line means we wait for
// cat's process to end instead of node's process.
$process = new Process('node user-update.js parameters | cat');