Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop fopen from triggering warning when attempting to open an invalid/unreachable URI

Tags:

php

I'm using fopen to generate a price feed.

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

}

Is there a way to stop this warning if the feed fails:

Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in…

like image 400
Regan Shepherd Avatar asked Jul 12 '11 02:07

Regan Shepherd


2 Answers

You can use @ to suppress the warning:

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

}

This is suitable here because you are handling the error condition appropriately. Note that liberal use of the @ sign, in general, to suppress errors and warnings is ill-advised.

Per the manual entry for fopen:

If the open fails, an error of level E_WARNING is generated. You may use @ to suppress this warning.

like image 141
Mark Elliot Avatar answered Sep 18 '22 18:09

Mark Elliot


here is another possible solution

$file_name = "http://feedurl";
if (file_exists($file_name) === false) {
    return;
}
$handle = fopen($file_name, "r");
like image 40
yellowhat5 Avatar answered Sep 21 '22 18:09

yellowhat5