Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether returned file is XML or not

Tags:

php

curl

xml

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

like image 638
Maks Avatar asked Feb 09 '12 13:02

Maks


2 Answers

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';
}
like image 59
Oldskool Avatar answered Oct 10 '22 08:10

Oldskool


$result = simplexml_load_string ($data, 'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
if (false == $result) echo 'error';
like image 28
akond Avatar answered Oct 10 '22 10:10

akond