-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行:分析器錯誤:開始標記預期, '<' 未發現
我哪裏錯了?
在此先感謝!
結賬http://www.php.net/manual/en/function.xml-parse.php – NotGaeL
你是什麼意思的「不工作」?請編輯您的問題並澄清。另外:請接受你的問題的答案! –
簡單:當'simplexml_load_file'給出錯誤時,該文件無效。 – Gordon