这段代码可以完美地与任何其他rss提要一起工作,但不能与谷歌新闻提要一起工作。我不知道我做错了什么,我想这是一些错误。当我尝试阅读谷歌新闻订阅时,我一直得到这个错误
This XML document is invalid, likely due to invalid characters. XML error: SYSTEM or PUBLIC, the URI is missing at line 1, column 61
例如,如果我们尝试http://stackoverflow.com/feeds
提要,它工作得很好,但不与谷歌新闻提要。谁能给我点提示吗?
<?php
//get the simplepie library
require_once('simplepie.inc');
//grab the feed
$feed = new SimplePie();
$feed->set_feed_url("http://news.google.com/news?hl=en&gl=us&q=austria&ie=UTF-8&output=rss");
$feed->force_feed(true);
//$feed->encode_instead_of_strip(true);
//enable caching
$feed->enable_cache(true);
//provide the caching folder
$feed->set_cache_location('cache');
//set the amount of seconds you want to cache the feed
$feed->set_cache_duration(1800);
//init the process
$feed->init();
//let simplepie handle the content type (atom, RSS...)
$feed->handle_content_type();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>simple</title>
</head>
<body>
<div id="page-wrap">
<h1>News Finder</h1>
<?php if ($feed->error): ?>
<p><?php echo $feed->error; ?></p>
<?php endif; ?>
<?php foreach ($feed->get_items() as $item): ?>
<div class="chunk">
<h4 style="background:url(<?php $feed = $item->get_feed(); echo $feed->get_favicon(); ?>) no-repeat; text-indent: 25px; margin: 0 0 10px;"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h4>
<p class="footnote">Source: <a href="<?php $feed = $item->get_feed(); echo $feed->get_permalink(); ?>"><?php $feed = $item->get_feed(); echo $feed->get_title(); ?></a> | <?php echo $item->get_date('j M Y | g:i a T'); ?></p>
</div>
<?php endforeach; ?>
</div>
确保你使用的是SimplePie 1.2.1, 1.2有一个URL解析的错误,可能会导致这种类型的错误。
(我也是SimplePie的首席开发人员,所以请直接向我的电子邮件发送问题)
如果您使用的是1.2.1,那么这似乎是错误#162的表现,目前尚未得到证实。我将对此进行深入研究,但这似乎肯定是SimplePie中的错误,而不是您的代码。
(我也会回到这里,告诉你们为什么会发生这种情况。)
我不知道SimplePie,但是,在您的情况下,简单的方法可能只是SimpleXML:
$url = "http://news.google.com/news?hl=en&gl=us&q=austria&bav=on.2,or.r_gc.r_pw.,cf.osb&biw=1920&bih=973&um=1&ie=UTF-8&output=rss";
$feed = simplexml_load_file($url);
echo $feed->channel->title, "n<", $feed->channel->link, ">nn";
foreach($feed->channel->item as $item)
{
echo "* $item->titlen <$item->link>n";
}
SimpleXML通常在PHP中直接可用,你不需要安装任何库。
演示对于Google News feed使用:
$feed->set_raw_data(file_get_contents($rssurl));
只是想在这里为那些认为上述答案不起作用的人添加一个注释。如果你在项目标题上得到一个null,检查提要源,这可能不是你的simplepie或脚本有什么问题,而是你的浏览器设置它为null,因为标题项目标签内的html代码。