Check for a valid XML file PHP

With curl I dowload XML file and saved localy. How can I check this file is valid XML. After donloadedd I save to mysql information from this file.

    $url = '<URL to XML>';
    $local_path = "myxml.xml";
    $file_handle = fopen($local_path, "w");

    ob_start();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FILE, $file_handle);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 4s
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $result = curl_exec($ch);
    curl_close($ch);
    ob_end_clean();
    fclose($file_handle);

I tried this but not working.

if (filesize('myxml.xml')>0) {

    $str_xml = file_get_contents('myxml.xml');
    if(simplexml_load_string($str_xml)){
        echo 'XML';
    }else{
        echo 'NOT XML';
    }

Code returned this WRONG

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in

Where I wrong ?

Thanks in advance !

Answers


It is not the parsing of the XML that is wrong.

You are getting a misformed XML from the CURL retrieval. Do a echo of the $str_xml and see whats wrong with it!


Need Your Help

Mailgun lacks support for ASCII Spanish language characters (ñ)

node.js mailgun

Mailgun seems to reject any email containing Spanish language characters. Is this desired functionality, or an oversight? I've tried various uses of ñ and nothing seems to be accepted. If this is a...

iOS local notification userInfo always null

ios objective-c

I am trying to schedule a local notification with some custom data attached which I will then read when the user opens the app by clicking on the notification.