我将此代码/函数方法作为php中类的一部分:
function defaulthome(){
$fp = null;
$err ='';
$xml_parser = xml_parser_create();
$rss_parser = new Rssparser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://gulfnews.com/cmlink/business-rss-feed-1.446098?localLinksEnabled=false","r");
if(!$fp) $err = "Error reading RSS data.";
else {
$count = 0;
while ($data = fread($fp, 4096) && $count<10) {
xml_parse($xml_parser, $data, feof($fp)) or $err=xml_error_string(xml_get_error_code($xml_parser));
$count++;
}
}
fclose($fp);
xml_parser_free($xml_parser);
$content_sect2 = $this->tnjn->render('forms/landlords_prompt.phtml');
$context = array('content1_title'=>'Welcome to my website','content1_article'=>"test article", 'feeds'=>$err);
$output = $this->tnjn->render("default.phtml", $context);
return $output;
}
我没有得到结果,我的错误是空文件!有谁知道代码的哪一部分是问题?
非常感谢!!
答案 0 :(得分:0)
查看错误报告是否提供任何信息。 error_reporting(E_ALL);
答案 1 :(得分:0)
为什么要尝试使用fopen获取xml? 而是尝试使用simplexml_load_file();
获取xmlif (!$xml = simplexml_load_file("http://domain.com/rss.xml"))
{
echo('Unable to load or parse search results feed');
}
if (!count($entries = $xml->entry)) {
echo('No entry found');
}
for($i=0;$i<count($entries);$i++)
{
echo $entries->autor;
}