php:simplexml_load_file检查服务器状态

时间:2010-12-09 13:36:16

标签: php simplexml

我从服务器加载文件:

$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);

如果服务器关闭,我会收到错误:

Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed:...
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

如何检查文件是否到达?

1 个答案:

答案 0 :(得分:2)

来自manual page for simplexml_load_file

  

返回类SimpleXMLElement的对象,其属性包含XML文档中保存的数据。出现错误时,它将返回FALSE

这意味着你可以做到

$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);

// check what was returned
if(FALSE === $xml) {
    echo 'could not open file';
}