2010-12-09 97 views
0

我从服务器加载文件:PHP:使用simplexml_load_file检查服务器状态

$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 

如何检查是否到达文件?

回答

2

manual page for simplexml_load_file

Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return 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'; 
}