此代码可以与任何其他 rss feed 完美配合,但不能与 google news feed 配合使用。我不知道我做错了什么,我认为这是一些错误。 当我尝试阅读 Google 新闻源时,不断收到此错误
此 XML 文档无效,可能是由于字符无效。 XML 错误:SYSTEM 或 PUBLIC,第 1 行第 61 列缺少 URI
例如,如果我们尝试使用 http://stackoverflow.com/feeds
feeds,它会很好地工作,但不适用于 google news 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,那么这似乎是 bug #162 的表现形式。目前尚未证实。我将深入研究这一点,但这似乎肯定是 SimplePie 中的错误,而不是您的代码中的错误。
(对于你们当中好奇的人,我也会在这里发帖说明为什么会发生这种情况。)
关于php - simplepie 不解析 google 新闻 rss feed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8663744/