simplepie没有解析google news rss feed

时间:2011-12-29 03:35:24

标签: php rss simplepie

此代码可与任何其他RSS Feed完美配合,但不适用于Google新闻Feed。我不知道我做错了什么,我认为这是一些错误。 我在尝试阅读Google新闻Feed时一直收到此错误

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 Feed,则效果很好,但不适用于Google新闻Feed。有人可以给我一个暗示吗?

<?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 个答案:

答案 0 :(得分:4)

确保您使用的是SimplePie 1.2.1,1.2有一个网址解析错误,可能会导致此类错误。

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

如果你使用的是1.2.1,那么这似乎是bug #162的一种表现,目前尚未证实。我会深入研究一下,但它似乎肯定是SimplePie中的错误,而不是代码中的错误。

(我也会在这里回帖说明为什么会出现这种情况,因为你们中间很好奇。)

答案 1 :(得分:1)

我对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, ">\n\n";

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

SimpleXML通常可直接与PHP一起使用,您不需要安装任何库。

Demo

答案 2 :(得分:0)

对于Google新闻Feed使用:

$feed->set_raw_data(file_get_contents($rssurl));

答案 3 :(得分:0)

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