在继续之前,是否有使用php检查远程xml文件是否可访问的代码


is there a code to use php to check if a remote xml file is accessible before continuing ?

我正在获取一个远程xml文件$variable['test'] = 'http://someurl.com/test.xml';,但当文件服务器关闭时,我的整个脚本停止工作!

在试图获取文件之前,有没有办法验证文件是否可访问?

最简单的解决方案是编写一个简单的检查条件。类似于:

$var = "http://someurl.com/test.xml";
if($var != NULL){
    //Do the code if you can see the XML.
}
else{
    //The code if your external XML is unreachable.
}

如果不查看,就无法查看XML是否可用。您总是需要进行检查,然后确保您的代码足够健壮,可以处理不健壮的情况。

如果您使用的是simplexml_load_file之类的东西,您可以很容易地捕捉到这样的错误。

$XML = @simplexml_load_file($file);
if ($XML  === false) {
  // Action for the case XML is not present.
}