如何利用php执行nodejs文件
如何利用php 的exec执行nodejs文件? 如下执行却执行失败
public function execTask ($dirPath)
{
$serverPath = self::$serverPath;
exec("node httpd.js $serverPath",$info, $val);
var_dump($info);
echo "$val";
}
如果仅仅是
exec("node -v $serverPath",$info, $val);
却可以输出版本号,执行成功。求解答,谢谢。
ruben
10 years, 1 month ago
Answers
我也有类似的用法,所以除了有能读出消息的之外,还有能直接后台执行的功能,都是通过进程实现的
/**
* 返回的汉语文字信息在windows中是GB2312编码,需要手动改成UTF8
* iconv("GB2312","UTF-8",$read);
* @param $cmd
* @return array
*/
public function execFront($cmd)
{
$response = array();
$handle = popen("$cmd 2>&1", 'r');
while ($read = fread($handle, 20096)) {
if(!mb_detect_encoding($read, 'UTF-8', true))
iconv("GB2312","UTF-8",$read);
$response[] = trim($read);
}
pclose($handle);
flush();
return $response;
}
/**
* @param $cmd
*/
public function execBackend($cmd)
{
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start cmd /c ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
return;
}
napel
answered 10 years, 1 month ago