Windows上通过PHP exec()的WGET无法正常工作 [英] WGET on Windows through PHP exec() doesn't work

查看:120
本文介绍了Windows上通过PHP exec()的WGET无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't get wget to work when called from PHP through 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数据库中的某些数据.

The called URL is an action from a project based on Zend Framework that manipulates some data in a MySQL database.

执行上述操作时,仅创建"c:\ wgetout.txt",并且为空.

When the above is executed, only "c:\wgetout.txt" is created, and is empty.

设置如下:

  • Windows 7
  • XAMPP
  • PHP版本5.3.5
  • 此处
  • 获取wget的最新版本.
  • PHP safe_mode已关闭
  • Windows 7
  • XAMPP
  • PHP ver 5.3.5
  • wget latest version from here
  • PHP safe_mode is Off

wget安装在"C:\ Program Files(x86)\ GnuWin32 \ bin"中,并将其添加到Windows PATH变量中.

wget is installed in "C:\Program Files (x86)\GnuWin32\bin", and this is added to the Windows PATH variable.

我知道wget设置正在运行,因为运行上述exec参数(如回显所示)

I know the wget setup is working because when running the above exec parameter (as echoed)

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等).

in a command prompt, it runs fine, I get the expected results in the database, and both files "C:\wgetlog.txt" and "C:\wgetout.txt" are created, with the latter containing wget's output (process id, etc).

最新编辑

感谢Crontab的建议,使它能够正常工作,并使用了调用wget的绝对路径,并将其括在双引号中.

Got it working thanks to Crontab's suggestion and used the absolute path for calling wget, also enclosed it in double quotes.

使用WSH COM对象而不是普通的exec()来运行它.

Used WSH COM object to run it instead of plain exec().

此外,在Windows上,如果输出未定向到某个地方,则-b参数也不起作用.由于我对输出并不特别感兴趣,因此将其定向到> NUL 2>&1(这还包括错误).

Also, on Windows, the -b parameter doesn't work if the output isn't directed somewhere. As I'm not particularly interested in the output, I directed it to > NUL 2>&1 (this includes errors also).

我迅速创建了此功能来帮助我在Windows机器上测试我的项目并使wget工作,所以这里是万一有人发现它有用的情况:

I quickly made this function to help me test my project on a Windows machine and have wget working, so here it is, in case anyone finds it useful:

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),操作系统检查方法可能不是最好的,等等.

Please mind that it's customized for my own setup (absolute path to wget), it's for testing purposes only (only use the Windows machine for testing, the actual production machine runs Linux), the OS checking method might not be the best, etc.

推荐答案

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

Try using an absolute path to wget - it's probably not in your script's path. To avoid using the absolute path, you could try adding putenv("PATH=<whatever-paths-you-need-colon-delimited>"); in your script before you access wget. Another way would be to directly modify the PATH environment variable of whatever user is running your webserver (and hence whichever user runs the PHP interpreter).

这篇关于Windows上通过PHP exec()的WGET无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆