SimpleXMLElement failure workaround

Hello

I have a script that uses SimpleXMLElement to access an API from an external provider. The same script is used on many websites to provide real-time content.

It works well almost all the time, however when the external provider has an occasional problem, it prevents my page from loading properly, and causes server overload because multiple httpd processes never finish.

The code in question is

 $pFile = new SimpleXMLElement('http://api.XXX.com/a/XXX-api/xml-v2/ws-'.$number.'/q-'.$terms.'?pshid=XXX&ssty=1&cflg=r', null, true);

For example, last night for about 2 hours the API access string typed into a browser produced the following error:

502 Bad Gateway
nginx

I have tried entering the following snippet into the script but it didn’t work:

libxml_use_internal_errors(true); if (!$pFile) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } }

I have also been playing around with curl, however that would mean accessing the API twice, and there are already thousands of hits per day.
Any suggestions will be much appreciated.

Thanks
Mike

Sponsor our Newsletter | Privacy Policy | Terms of Service