当从PHP通过exec()
调用时,我无法使wget
工作。
代码为:
exec('wget -b --timeout=300 --no-check-certificate -O c:'wgetlog.txt http://localhost/project/someparam/somevalue > c:'wgetout.txt')
所谓的URL是来自一个基于Zend Framework的项目的操作,该项目处理MySQL数据库中的一些数据。
执行上述操作时,只会创建"c:''wgetout.txt",并且为空。
设置如下:
- Windows 7
- examplep
- PHP版本5.3.5
- wget最新版本
- PHP
safe_mode
关闭
wget安装在"C:''Program Files(x86)''GnuWin32''bin"中,并添加到Windows PATH变量中。
我知道wget设置正在工作,因为当运行上面的exec参数时(如回声)
wget -b --timeout=300 --no-check-certificate -O c:''wgetlog.txt http://localhost/project/someparam/somevalue > c:''wgetout.txt
在命令提示符下,它运行良好,我在数据库中得到了预期的结果,并创建了文件"C:''wgetlog.txt"answers"C:''wgetout.txt",后者包含wget的输出(进程id等)。
更新版:
多亏了Crontab的建议,它才工作起来,并使用了调用wget
的绝对路径,还用双引号括起来。
使用WSH COM对象来运行它,而不是使用普通的exec()
。
此外,在Windows上,如果输出没有指向某个地方,-b
参数将不起作用。由于我对输出不是特别感兴趣,所以我将其定向到> NUL 2>&1
(这也包括错误)。
我很快就制作了这个功能,以帮助我在Windows机器上测试我的项目,并使wget
正常工作,所以它就在这里,以防有人觉得它有用:
public function execWget($URL, $intTimeout = 30, $blnInBackground = true) {
if (preg_match("/Win/i", PHP_OS)) {
$runCommand = '"C:'Program Files (x86)'GnuWin32'bin'wget" ' . ($blnInBackground?'-b ':'') . '--timeout=' . (int)$intTimeout . ' --no-check-certificate ' . $URL . ($blnInBackground?' > NUL 2>&1':'');
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($runCommand, 7, false);
} else {
$runCommand = 'wget ' . ($blnInBackground?'-b ':'') . '--timeout=' . (int)$intTimeout . ' --no-check-certificate ' . ($blnInBackground?'-O /dev/null ':'') . $URL . ($blnInBackground?' > /dev/null 2>&1':'');
exec($runCommand);
}
}
请注意,它是为我自己的设置定制的(wget
的绝对路径),仅用于测试目的(仅使用Windows机器进行测试,实际生产机器运行Linux),操作系统检查方法可能不是最好的,等等。
尝试使用wget
的绝对路径-它可能不在脚本的路径中。为了避免使用绝对路径,可以在访问wget
之前尝试在脚本中添加putenv("PATH=<whatever-paths-you-need-colon-delimited>");
。另一种方法是直接修改运行Web服务器的任何用户(以及运行PHP解释器的任何用户)的PATH环境变量。