2010-12-09 76 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'; 
}