I need to check the return value of a website output. In case of valid login details it returns a XML file and in case of invalid login details it just returns a string saying "You entered a invalid id".
My problem is I have used this code to check,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
if(simplexml_load_string($output)){
echo 'Is XML';
}else{
echo 'Not XML';
}
The problem is it is checking and displaying the message, but I am getting these errors
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in
Warning: simplexml_load_string() [function.simplexml-load-string]:
Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in
Is there a way to sort out these errors. I have been searching in the internet for the past hour without any success.
Thanks
Check if the first 5 characters equal <?xml
, that should be a pretty good clue.
if(substr($output, 0, 5) == "<?xml") {
echo 'Is XML';
} else {
echo 'Not XML';
}
$result = simplexml_load_string ($data, 'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
if (false == $result) echo 'error';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With