edzillion Posted March 13, 2008 Share Posted March 13, 2008 my setup is this I have a php file that displays prices, it calls another php file that gets the xml file from a URL: pricefeed.php: <?php $fileUrl = "http://someurl.com/prices.xml"; $AgetHeaders = @get_headers($fileUrl); if (preg_match("|200|", $AgetHeaders[0])) { // file exists echo"file exists"; readfile($fileUrl); } else { // file doesn't exist echo"file doesn't exist"; return false; } ?> If I run this when the url is unavailable I get "file doesn't exist", but I can't figure out how to call this file without a parsing error if the file is unavailable. The calling line: $xml = simplexml_load_file('http://localhost/scripts/pricefeed.php'); Gives the error: Warning: simplexml_load_file() [function.simplexml-load-file]: http://localhost/scripts/pricefeed.php:1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\prices.php on line 17 If I get rid of the "file exists" echoes from the pricefeed.php file I get Document is empty in C:\xampp\htdocs\prices.php on line 17 Any ideas? TIA Ed Quote Link to comment https://forums.phpfreaks.com/topic/95936-simplexml-loading-from-url-problem/ Share on other sites More sharing options...
Daniel0 Posted March 14, 2008 Share Posted March 14, 2008 [...] but I can't figure out how to call this file without a parsing error if the file is unavailable. That doesn't make sense. You obviously cannot parse a file which doesn't exist... Quote Link to comment https://forums.phpfreaks.com/topic/95936-simplexml-loading-from-url-problem/#findComment-491999 Share on other sites More sharing options...
edzillion Posted March 14, 2008 Author Share Posted March 14, 2008 true. So pricefeed returns false when the file doesn't exist. So how do I handle this error without outputting a php error? I can't seem to get a try catch statement to catch the exception. try { $xml = simplexml_load_file('http://localhost/scripts/pricefeed.php'); } catch (Exception $e) { // handle the error echo $e->getMessage(); } Just returns the error: Warning: simplexml_load_file() [function.simplexml-load-file]: http://localhost/scripts/pricefeed.php:1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\wpgoldprices.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/95936-simplexml-loading-from-url-problem/#findComment-492161 Share on other sites More sharing options...
samshel Posted March 14, 2008 Share Posted March 14, 2008 Hi, Or you can simply do this: <?php $fileUrl = "http://someurl.com/prices.xml"; $AgetHeaders = @get_headers($fileUrl); if (preg_match("|200|", $AgetHeaders[0])) { // file exists echo"file exists"; readfile($fileUrl); } else { // file doesn't exist echo"<error>Cannot read file</error>"; return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95936-simplexml-loading-from-url-problem/#findComment-492167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.