A wrapper around simplexml_load_file to circumvent nasty error messages when the xml server times out or gives a 500 error etc.
<?php
function loadXML2($domain, $path, $timeout = 30) {
$fp = fsockopen($domain, 80, $errno, $errstr, $timeout);
if($fp) {
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $domain\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$resp = "";
while (!feof($fp)) {
$resp .= fgets($fp, 128);
}
fclose($fp);
$status_regex = "/HTTP\/1\.\d\s(\d+)/";
if(preg_match($status_regex, $resp, $matches) && $matches[1] == 200) {
$parts = explode("\r\n\r\n", $resp);
return simplexml_load_string($parts[1]);
}
}
return false;
}
?>