如何将数据从PHP传递给Node?
这就是我现在正在尝试的:
PHP
shell_exec('node bin/gfm.js '.escapeshellarg($code))
Node.js的
console.log(process.argv[2]);
但是Node似乎只是接收$code
的第一行 ,所以看起来escapeshellarg
并没有正确地逃避换行。
我还能怎么做呢? 如果更简单,我可以使用stdin
,但是在PHP和Node上看起来都很复杂。
这花了一些手段,但我想我明白了:
PHP
$spec = array( 0 => array("pipe", 'r'), 1 => array("pipe", 'w'), ); $proc = proc_open('node bin/gfm.js', $spec, $pipes); fwrite($pipes[0], $code); fclose($pipes[0]); $resp = stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($proc); echo $resp;
Node.js的
var fs = require('fs'); var size = fs.fstatSync(process.stdin.fd).size; var buffer = size > 0 ? fs.readSync(process.stdin.fd, size)[0] : ''; console.log(buffer);
信用: 西格蒙德