How to stop fopen from starting a warning when trying to open an invalid / unreachable URI

I use fopen to generate prices.

if (($handle = fopen("http://feedurl", "r")) !== FALSE) {

}

Is there a way to stop this warning if the feed is not working:

Warning : fopen () [function.fopen]: php_network_getaddresses: getaddrinfo failed: name or service unknown in ...

+5
source share
3 answers

You can use @to suppress a warning:

if(($handle = @fopen("http://feedurl", "r")) !== FALSE){

}

This is suitable here because you are handling the error condition correctly. Please note that liberal use of the mark @is generally not recommended for suppressing errors and warnings.

In manual input forfopen :

, E_WARNING. @ .

+13

$file_name = "http://feedurl";
if (file_exists($file_name) === false) {
    return;
}
$handle = fopen($file_name, "r");
+2

Or you can use

error_reporting(E_ALL ^ E_WARNING);
-2
source

All Articles