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


php file() handle errors when it goes something wrong

我使用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_network_getaddresses: getaddrinfo failed: Name or service not known in

2)打开流失败:php_network_getaddresses: getaddrinfo failed: Name or service not known in

var_dump($XMLContent);显示假

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

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

$file = fopen ("http://www.example.com/", "r");
if (!$file) {
    echo "<p>Unable to open remote file.'n";
    exit;
}

来源:http://php.net/manual/en/features.remote-files.php