-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