开发者

php: simplexml_load_file check server status

开发者 https://www.devze.com 2023-01-30 06:17 出处:网络
i load file from server: $url = \'http://www.sample.com/test.xml\'; $xml = simplexml_load_file($url); And if servers is close i get error:

i load file from server:

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

And if servers is close i get error:

Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed:...
Warning: simplexml_load_file() [funct开发者_如何学JAVAion.simplexml-load-file]: I/O warning : failed to load external entity

how to check if the file is reached?


From the 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.

which means you can do

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

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

精彩评论

暂无评论...
验证码 换一张
取 消