dsbqfrr098575666 2017-03-05 22:07
浏览 56

从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
  • 写回答

1条回答 默认 最新

  • duanan6043 2017-03-06 14:00
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏