I can't get wget
to work when called from PHP through exec()
.
The code is:
exec('wget -b --timeout=300 --no-check-certificate -O c:\wgetlog.txt http://localhost/project/someparam/somevalue > c:\wgetout.txt')
The called URL is an action from a project based on Zend Framework that manipulates some data in a MySQL database.
When the above is executed, only "c:\wgetout.txt" is created, and is empty.
The setup is as follows:
- Windows 7
- XAMPP
- PHP ver 5.3.5
- wget latest version from here
- PHP
safe_mode
is Off
wget is installed in "C:\Program Files (x86)\GnuWin32\bin", and this is added to the Windows PATH variable.
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
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).
LATER EDIT:
Got it working thanks to Crontab's suggestion and used the absolute path for calling wget
, also enclosed it in double quotes.
Used WSH COM object to run it instead of plain exec()
.
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).
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);
}
}
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.