Check whether returned file is XML or not
Solution 1
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';
}
Solution 2
$result = simplexml_load_string ($data, 'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
if (false == $result) echo 'error';
Solution 3
Could also be because the first line of the return data is blank - try $output = trim($output)
before you parse the string as XML.
Maks
Updated on June 12, 2022Comments
-
Maks almost 2 years
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