In PHP, I am using get_meta_tags()
and get_headers()
, however, when there is a 404, those two functions throw a warning. Is there any way for me to catch it?
Thanks!
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You can silence it by calling them like this:
You can't "catch" it (easily), but you can check the return values.
Also, you can disable or redirect warnings, see error_reporting() and ini directoves "display_errors" & similar.
You shouldn't have to care. Naturally, a E_WARNING message upon failure while developing is fine; it's even desirable, as you can instantly see that something went wrong. I can imagine though that you don't want your customers to see those warnings, but you should not be doing that per use of function, you should be doing that globally: turn display_errors off in the php.ini in the production environment, and your customers will never see such messages.
That said, if you don't want them to appear in the error logs, you'll have to check to see if the page exists before trying to retrieve the meta tags. get_headers doesn't appear to throw a warning, instead it returns an array of which the first element contains the string "HTTP/1.1 404 Not Found". You can use this to your advantage:
If you start using this code, mind that 200 isn't the only notification of a successful request; 304 Not Modified - for example - is equally valid.
get_headers
does not throw a Warning/Error on 404, butget_meta_tags
does.So you can check the header response and do something, when it's not OK: