I'm running wget through php's shell_exec() How could I understand that wget got 404 error g开发者_如何学Pythonetting file?
Thank you.
I think you are solving the wrong problem. Please try CURL before using anything that can't fail portably with a meaningful, parse-able status as David first suggested in comments.
According to this, when faced with a simple "download file XYZ" operation, wget will return a return code of 1 when the download fails for some reason. It won't work for complex operations like mirroring a site, but in your case, it should be fine.
You can get hold of the return code using exec()`s "return_var" variable.
Wget should return 1
(or some other status code that is not 0
) if it is presented with a proper 404 page. Try it out.
To actually distinguish the exact error status code that wget receives, I think you'll have to start parsing its output (or use a PHP-based web client class.)
You can use a command line like the following with exec
:
exec("wget http://www.google.com/not_present.html 2> some_temp_file ",&output,&retvalue);
Check the retvalue
to see if an error has occurred. A non-zero value means error occurred. Since we've redirected the standard error to some_temp_file
, you can grep this file for the pattern 404 Not Found
. If present it indicates that a 404
has occurred.
精彩评论