通过PHP exec()在Windows上WGET不起作用

时间:2012-03-27 11:56:45

标签: php windows exec wget

从PHP通过wget调用时,我无法使exec()工作。

代码是:

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
  • XAMPP
  • PHP ver 5.3.5
  • 来自here
  • 的最新版本
  • 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”和“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),操作系统检查方法可能不是最好的等等。

1 个答案:

答案 0 :(得分:1)

尝试使用wget的绝对路径 - 它可能不在您的脚本路径中。为避免使用绝对路径,您可以尝试在访问putenv("PATH=<whatever-paths-you-need-colon-delimited>");之前在脚本中添加wget。另一种方法是直接修改运行Web服务器的任何用户的PATH环境变量(因此无论哪个用户运行PHP解释器)。