No response from XML using cURL

I am using a simple cURL statement to parse XML on my site. When the API is up and working it works fine, however as soon as the API does down for any reason the entire site crashes.

$url = 'http://www.mydomain.com/webservicexample';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch); 

$xml = new SimpleXmlElement($data);

Is there a conditional I can put around the url so that it only carries out the cURL script if there’s a positive response from the API? I tried the following but it didn’t work because it never got a server response to give any headers:

$url_headers = @get_headers($url);
if($url_headers[0] == 'HTTP/1.1 200 OK') {

    // do script
}

Any help/advice much appreciated!

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories