[英]Run Node.js script from PHP - output is truncated to 512 characters
我们使用Symfony Process从PHP运行node.js CLI脚本。
脚本始终将整个响应作为JSON打印在一行中。
响应以某种方式被截断为512个字符。
我只在php.ini
发现xdebug.var_display_max_data => 512 => 512
,但是看不到它是如何关联的。
Adapter > Symfony Process > node script.js
A)测试节点脚本
$ node user-update.js parameters
返回完整结果-例如629个字符。 B)测试Symfony过程
$process = new Process($cmd);
try {
$process->mustRun();
$response = $process->getOutput();
} catch (ProcessFailedException $e) {
$response = $e->getMessage();
}
echo $response;
echo PHP_EOL;
echo strlen($response);
$cmd = 'node user-update.js parameters';
-截断为512。 $cmd = 'php -r \\'for($i=0; $i<520; $i++){ echo "."; }\\'';
-不截断。 $cmd = 'cat long_one_line.txt';
-打印完整文件。 一行中包含1650个字符。 C)尝试使用PHP Shell函数
$response = shell_exec($cmd); // response is truncated to 512
system($cmd, $returnVal); // print directly to STDOut, truncated to 512
可能是什么原因和解决方案?
我怀疑您的进程在PHP可以读取缓冲区之前就结束了。
作为解决方法,您可以添加以下内容:
// 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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.