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

url action is not working in my rails

ruby-on-rails ruby ruby-on-rails-3 url ruby-on-rails-3.2

I made a salaries controller inside the folder employee.

How to extract the adjacency matrix from an image in matlab?

image matlab extract

I want to know how to extract the adjacency matrix from an image in matlab.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.