2012-09-10 61 views
-1

使用curl我下載XML文件並保存到本地。我如何檢查這個文件是否是有效的XML。 donloadedd後,我保存到這個文件的MySQL信息。檢查有效的XML文件PHP

$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); 

我試過這個,但沒有工作。

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

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

返回代碼本WRONG

警告:simplexml_load_string()[function.simplexml負荷字符串]:實體:第1行:分析器錯誤:開始標記預期, '<' 未發現

我哪裏錯了?

在此先感謝!

+0

結賬http://www.php.net/manual/en/function.xml-parse.php – NotGaeL

+1

你是什麼意思的「不工作」?請編輯您的問題並澄清。另外:請接受你的問題的答案! –

+0

簡單:當'simplexml_load_file'給出錯誤時,該文件無效。 – Gordon

回答

0

這不是解析錯誤的XML。

您正在從CURL檢索中獲取一個不正確的XML。做一個$str_xml的回聲,看看它有什麼問題!

+0

有些情況不會返回有效的XML。我只想檢查下載的文件是否是XML文件 – dido