Jump to content

simplexml_load_file: handling errors


rossmurphy

Recommended Posts

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

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.

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.

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?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.