-1

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 !

dido
  • 2,330
  • 8
  • 37
  • 54

1 Answers1

0

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!

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
  • There are cases that do not return a valid XML. I just want to check whether the downloaded file is XML or not – dido Sep 10 '12 at 08:33