tags:

views:

1261

answers:

1

I have a PHP-driven site that includes an XML stock feed, which is remotely served from ASP (i.e. the XML feed url is of the order: http://remote.com/client.asp).

As the feed is often unavailable (by which I mean the site returns an ASP error) I'd like to check if the feed is well-formed XML before including it. My usual url_exists function doesn't do the trick as of course the URL does exist even when 'erroring'.

TIA.

+7  A: 

Use cURL to get the result and simplexml to check if the XML is well-formed.

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://remote.com/client.asp");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
if (simplexml_load_string($output)) {
  // well-formed XML
} else {
  // it isn't
}
cletus
Thanks. I'm already using something very similar to that, and it is this script that often throws an error. Though I'm using simplexml_load_file, would there be a difference with simplexml_load_string?
da5id
Apparently so as all is now good. Thanks again, marking answer as accepted.
da5id