0

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
Vladimir Vukanac
  • 944
  • 16
  • 29
  • I would begin from debug of nodejs app. – michail_w Mar 05 '17 at 22:18
  • It returns the full result as expected from the terminal. And it works correctly from the main PHP method if the requested less long result. Eg. if AuthUser does not have `photoURL` nor long `dispalyName`, than the response is less than 512 chars, and the Process (PHP) get whole valid JSON - everything works correctly. – Vladimir Vukanac Mar 05 '17 at 22:27
  • `PHP 7.1.2 (cli) (built: Feb 23 2017 23:40:22) ( NTS )` – Vladimir Vukanac Mar 05 '17 at 22:29
  • Dirty fix is to run `$cmd = 'node script.js >' . $tmp;`, than `cat $tmp;` or read it with php, and delete $tmp file. Hope for a real answer. – Vladimir Vukanac Mar 05 '17 at 22:53
  • Removing `xdebug` nothing is changed. – Vladimir Vukanac Mar 05 '17 at 22:54
  • Is the output of node command one line string? – michail_w Mar 05 '17 at 23:04
  • Yes. The script always print whole response as JSON in one line. – Vladimir Vukanac Mar 05 '17 at 23:10
  • First try (I assume you're running it under Unix?): please show me the result of (run it under bash, and from PHP - we'll compare results) `echo "Columns: " $COLUMNS` – michail_w Mar 05 '17 at 23:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137299/discussion-between-michail-w-and-vladimir-vukanac). – michail_w Mar 05 '17 at 23:14
  • What version of node are you using? Update to the latest. There have been issues with processes ending before stdio is flushed, and the output handoff between processes may end when the process dies. – Incognito Mar 06 '17 at 13:57

1 Answers1

0

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');
Incognito
  • 20,537
  • 15
  • 80
  • 120