请问用php怎么才能正确读取http动态的XML文件?
例如 文件地址是
http://www.donnerwetter.de/extern/div4/europ-assistance617-suche.xml?search=pad我试过用fopen打开解析,没有报错,但是解析不出其中的数据。如果我把xml数据保存下来,然后再用fopen打开这个本地的xml文件就能正确解析出来。PHP.ini远程访问设置已经打开。请各位给小弟一点建议,我已经找了几天了,也不知道为什么,请大家看看,谢谢。
例如 文件地址是
http://www.donnerwetter.de/extern/div4/europ-assistance617-suche.xml?search=pad我试过用fopen打开解析,没有报错,但是解析不出其中的数据。如果我把xml数据保存下来,然后再用fopen打开这个本地的xml文件就能正确解析出来。PHP.ini远程访问设置已经打开。请各位给小弟一点建议,我已经找了几天了,也不知道为什么,请大家看看,谢谢。
第4行,就是我保存下来的xml数据,存在这个文件中,就能正确解析,但是如果是第2行,直接打开就不行了。<?php
$parser = xml_parser_create(); //创建一个parser编辑器
$file = fopen ("http://www.donnerwetter.de/extern/div4/europ-assistance617-suche.xml?search=pad", "r");
//$file = fopen ("wetterinfo.xml", "r");xml_set_element_handler($parser, "startElement", "endElement");//设立标签触发时的相应函数 这里分别为startElement和endElenment
xml_set_character_data_handler($parser, "characterData");//设立数据读取时的相应函数
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
while ($data = fread($file, 4096)) { /* This only works if the title and its tags are on one line */ xml_parse($parser, $data,feof($file)); echo $data;
}fclose($file);xml_parser_free($parser);//关闭和释放parser解析器$Nummer=false;
$ID=false;
$Name=false;function startElement($parser_instance, $element_name, $attrs) //function of tag start
{
global $Nummer,$ID,$Name; if($element_name=="Nummer")
{
$Nummer=true;
$ID=false;
$Name=false;
echo " Nummer: ";
} if($element_name=="ID")
{$Nummer=false;
$ID=true;
$Name=false;
echo " ID: ";
} if($element_name=="NAME")
{$Nummer=false;
$ID=false;
$Name=true;
echo " Name: ";
}
}
//echo "wetter ist";function characterData($parser_instance, $xml_data) //function of the XML file content
{
global $Nummer,$ID,$Name; if($Nummer) echo $xml_data."<tr>";
if($ID) echo $xml_data."<tr>";
if($Name) echo $xml_data."<tr> <br>";
}function endElement($parser_instance, $element_name) //function of tag end
{
global $Nummer,$ID, $Name;
$Nummer=false;
$ID=false;
$Name=false;
}?>