views:

425

answers:

2

If i am making multiple calls to an api on each page in a web app, is cURL right for me, or is using simplexml_load_file with the URL wrapper OK?

Will opening, making the call, and closing the cURL connection multiple times negatively affect the app?

+1  A: 

You should be fine. When you use simplexml_load_file with URL wrappers it's doing things very similar to cURL, its just that PHP is managing the opening and closing of the connections for you. In fact, conventional wisdom is that cURL will handle connections in a more efficient manner than using simplexml_load_file with URL wrappers. Additionally, not all shared hosts support using simplexml_load_file with URLs, so you often have to fall back on cURL anyways.

Alan Storm
curl isn't always available either...
Jordan Ryan Moore
True, but I'd say if you're somewhere that has both URL Wrappers off AND curl disabled and you need to fetch XML resource over HTTP, you're kinda screwed :)
Alan Storm
+1  A: 

Well first of all i wouldnt ge tinto the habit of making api calls with the url loading functions of simpleXML and DOMDocument... Youre better off using cURL, file_get_contents, fopen, etc.. That way you can catch any issues with the request/response before you get the parsing part, ie. youve got a better ability to handle errors and direct control flow and log/present more meaningful messages about whats going wrong.

With that said i would never create and destroy multiple cURL resource handlers (if thats what you mean) jsut open a single one and then keep resetting the attributes and dispatching a new request.

As far as which one is actually faster or less resource intensive, i think thats the least of the issues when compared to the other things i mentioned above... but then thats jsut my opinion i suppose.

prodigitalson
Thanks for that, I enabled cURL for the fact that i can log errors and find out why and if the api has timed out.
Ross
No problem... BTW you my want to check out some of the http client packages that are out there in the FOSS world... they really tend to help with creating all thsi functionality instead of jsut using the raw php cURL functions. You might want to look at Zend_Http_Client or sfWebBrowserPlugin (though with the later youll have to do some modifying to remove or integrate the dependencies it has). Have fun!
prodigitalson