2

此代码与任何其他 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>

4

4 回答 4

4

确保您使用的是SimplePie 1.2.1,1.2的 URL 解析存在错误,可能导致此类错误。

(我也是 SimplePie 的首席开发人员,所以请随时直接向我的电子邮件提出问题)

如果您使用的是 1.2.1,这似乎是bug #162的表现,目前尚未确认。我将对此进行深入研究,但这似乎肯定是 SimplePie 中的错误,而不是您的代码中的错误。

(我也会在这里发帖说明为什么会发生这种情况以引起你们的好奇。)

于 2011-12-29T04:37:08.750 回答
1

I have no clue about SimplePie, however, the simple way in your case might be just 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, ">\n\n";

foreach($feed->channel->item as $item)
{
    echo "* $item->title\n  <$item->link>\n";
}

SimpleXML is normally directly available with PHP, you don't need to install any library or so.

Demo

于 2011-12-29T04:14:43.380 回答
0

对于 Google 新闻提要使用:

$feed->set_raw_data(file_get_contents($rssurl));
于 2016-09-07T20:18:29.330 回答
0

只是想在这里为其他认为上述答案不起作用的人添加一个注释。如果您的项目标题为空,请检查提要源,您的 simplepie 或脚本可能没有任何问题,但您的浏览器将其设置为空,因为标题项目标签中的 html 代码。

于 2018-01-05T21:00:21.827 回答