开发者

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

I'm using fopen to generate a price feed.

if (($handle = fopen("h开发者_开发问答ttp://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…


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.


here is another possible solution

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


Or you can use

error_reporting(E_ALL ^ E_WARNING);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜