【发布时间】:2016-03-18 18:24:41
【问题描述】:
我正在尝试使用 shell_exec 函数从 php 调用 gulp。
每次我尝试这样做时,都会得到一个空响应。
以下是一些有效和无效的示例:
//$output = shell_exec("whoami"); // ok
//$output = shell_exec("ls"); // ok
//$output = shell_exec("git --version"); // ok
//$output = shell_exec("id"); // ok
//$output = shell_exec("which php"); // ok
//$output = shell_exec("php --version"); // ok
//$output = shell_exec("which gulp"); // Doesn't work
//$output = shell_exec("gulp --version"); // Doesn't work
//$output = shell_exec("/usr/local/bin/gulp --version"); // Doesn't work
//$output = shell_exec("which node"); // Doesn't work
//$output = shell_exec("node --version"); // Doesn't work
$output = shell_exec("/usr/local/bin/node --version"); // ok
我正在为我的开发环境使用 vagrant 机器。
我找不到方法来检查 gulp 无法被 php 执行的根本原因。
当我使用 exec 时,我得到一个空响应和以下代码:
//exec("which gulp", $output, $code); // Code 1
//exec("gulp --version", $output, $code); // Code 127
exec("/usr/local/bin/gulp --version", $output, $code); // Code 127
有什么想法吗?
提前致谢。
编辑:
刚刚将 2>&1 添加到一些调用中并得到了这个:
$output = shell_exec("which gulp 2>&1"); // Doesn't work
// {"output":"which: no gulp in (\/sbin:\/usr\/sbin:\/bin:\/usr\/bin)\n"}}
$output = shell_exec("gulp --version 2>&1"); // Doesn't work
// {"output":"sh: gulp: command not found\n"}}
$output = shell_exec("/usr/local/bin/gulp --version 2>&1"); // Doesn't work
// {"output":"\/usr\/bin\/env: node: No such file or directory\n"}}
编辑:
这样解决了:
putenv('PATH=/usr/local/bin/');
在调用 shell_exec 之前必须手动将 gulp 路径推送到 php。
希望对你有帮助。
【问题讨论】:
-
在执行命令时,您是否直接从您的盒子中获得正常响应?
-
嗨 Muhammed M,是的,当我从机器内部和命令行执行 gulp 命令时,它工作正常。