这是一个简单的PHP代码
<?php
ob_start(passthru('/usr/bin/env node getResult.js '));
$data = ob_get_contents();
ob_end_clean();
// console shows Data as String but $data is still empty
echo "Data : " . $data;
?>
node.js 脚本只有一个变量结果,其中包含一个对象。
console.log("skript startet");
var get = function(){
/*do stuff to get variable*/
result = "test;
console.log(result);
return result;
});
});
};
get();
问题:我需要 getResult.js 脚本中的变量,但我无法在 php 中捕获它。有什么想法吗?
最佳答案
为什么要为输出缓冲区烦恼呢?
exec( '/usr/bin/env node getResult.js', $data );
echo "Data: " . $data;
exec()
将命令的完整输出写入提供的变量中。
关于javascript - 无法从 passthru 得到答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35793794/