rossmurphy Posted December 10, 2009 Share Posted December 10, 2009 In my app i call an API using this: $xml = simplexml_load_file("http://123.456.678.4/getroom.action?val=1&key=dfdf"); In the event that the API is down or the connection timesout, i want to be able to throw an exception. Is there a way to check for this file before running the simplexml_load_file so that i can prevent errors been thrown. Thanks. Link to comment https://forums.phpfreaks.com/topic/184700-simplexml_load_file-handling-errors/ Share on other sites More sharing options...
trq Posted December 11, 2009 Share Posted December 11, 2009 simplexml_load_file like most functions returns false upon failure. Link to comment https://forums.phpfreaks.com/topic/184700-simplexml_load_file-handling-errors/#findComment-975249 Share on other sites More sharing options...
rossmurphy Posted December 11, 2009 Author Share Posted December 11, 2009 I understand that it returns false. But it still produces warnings: [function.simplexml-load-file]: failed to open stream: Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity Is this normal behavior? Is there a better way of loading xml? Thanks. Link to comment https://forums.phpfreaks.com/topic/184700-simplexml_load_file-handling-errors/#findComment-975438 Share on other sites More sharing options...
rossmurphy Posted December 11, 2009 Author Share Posted December 11, 2009 Would anyone suggest using cURL combined with simplexml?? I know i could do timeouts with this. What have other people done out there? Link to comment https://forums.phpfreaks.com/topic/184700-simplexml_load_file-handling-errors/#findComment-975462 Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 You could get timeouts regardless of the method used. The fact that you are getting that message probably means they are blocking you from accessing the data in the manner you have tried. cURL is probably the most likely solution to this problem. Link to comment https://forums.phpfreaks.com/topic/184700-simplexml_load_file-handling-errors/#findComment-975484 Share on other sites More sharing options...
rossmurphy Posted December 11, 2009 Author Share Posted December 11, 2009 So what would be more suitable for an API that i am calling a few times per page load. Is cURL more strenuous on the system? Would simplexml_load_file be more suited to this task? Can i ignore the warnings given by simplexml_load_file? The ultimate scenario i am trying to achieve here is for the app to make the call to the api, but in the event that the API is down for some reason, i want the app to handle it by displaying some information, rather than php warnings and errors. I would say an exception is the way to go. I just don't know if the php warnings are suppose to appear? Link to comment https://forums.phpfreaks.com/topic/184700-simplexml_load_file-handling-errors/#findComment-975541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.