php file()在出错时处理错误

php file()在出错时处理错误,php,Php,我使用php file()函数将xml内容放入数组中,如下所示: $XMLContent=file("http://www.domain.com/file.xml"); 但我不知道如何处理错误,如如果文件将不存在或网站。 我试着用这种方式: try { $XMLContent=file("http://www.domain.com/file.xml"); .... } catch (Exception $e) { echo $e->getMessage(); } 但如果网

我使用php file()函数将xml内容放入数组中,如下所示:

$XMLContent=file("http://www.domain.com/file.xml");
但我不知道如何处理错误,如如果文件将不存在或网站。 我试着用这种方式:

try {
  $XMLContent=file("http://www.domain.com/file.xml");
  ....
} catch (Exception $e) {
   echo $e->getMessage();
}
但如果网站出错,我的错误日志中总会出现两个错误:

1) file():php\u network\u getaddresses:getaddrinfo失败:中不知道名称或服务

2) 无法打开流:php\u network\u getaddresses:getaddrinfo失败:中未知名称或服务

var_dump($XMLContent)显示错误

如果文件不存在,那么我的日志中不会出现错误,但是
var_dump($XMLContent)显示我不需要的其他东西

如何正确处理这些错误,并且这些错误不会记录在错误文件中?

$file=fopen(“http://www.example.com/“,“r”);
$file = fopen ("http://www.example.com/", "r");
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
}
如果(!$file){ echo“无法打开远程文件。\n”; 出口 }
来源:

$file=fopen(“http://www.example.com/“,“r”);
如果(!$file){
echo“无法打开远程文件。\n”;
出口
}

来源:

这没有帮助,因为我可以这样做:
如果(!$XMLContent=file(“http://www.domain.com/file.xml“){
这没有帮助,因为我可以这样做:
如果(!$XMLContent=file(”http://www.domain.com/file.xml){