Php simplepie未解析google新闻rss源

Php simplepie未解析google新闻rss源,php,rss,simplepie,Php,Rss,Simplepie,此代码与任何其他rss源都能完美配合,但与google新闻源不配合。我不知道我做错了什么,我想这是某种错误。 当我试图阅读谷歌新闻提要时,我总是遇到这个错误 此XML文档无效,可能是由于无效字符。XML错误:SYSTEM或PUBLIC,第1行第61列缺少URI 例如,如果我们尝试http://stackoverflow.com/feeds feed它工作得很好,但不适用于谷歌新闻feed。谁能给我一个提示吗 <?php //get the simplepie library

此代码与任何其他rss源都能完美配合,但与google新闻源不配合。我不知道我做错了什么,我想这是某种错误。 当我试图阅读谷歌新闻提要时,我总是遇到这个错误

此XML文档无效,可能是由于无效字符。XML错误:SYSTEM或PUBLIC,第1行第61列缺少URI

例如,如果我们尝试http://stackoverflow.com/feeds feed它工作得很好,但不适用于谷歌新闻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>

我对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,您不需要安装任何库

请确保您正在使用,1.2在URL解析方面有一个bug,这可能会导致此类错误

我也是SimplePie的首席开发人员,所以可以直接向我的电子邮件提问

如果您使用的是1.2.1,那么这似乎是目前尚未确认的一种表现形式。我将对此进行深入研究,但这显然是SimplePie中的错误,而不是您的代码中的错误


我还将在这里发帖,告诉大家为什么会发生这种情况。

对于谷歌新闻提要的使用:

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

我只是想在这里为那些认为上述答案不起作用的人补充一点。如果您的项目标题为空,请检查提要源,您的simplepie或脚本可能没有任何问题,但您的浏览器将其设置为空,因为标题项目标记中包含html代码。

谢谢hakre,我在服务器上尝试了您的脚本,但它似乎不起作用。有什么要求吗?SimpleXML很好,只要您知道提要将符合RSS/Atom。问题是,互联网上的大多数订阅源都没有。SimplePie实际上是SimpleXML上的一个规范化层。@Ryan McCue:SimpleXML并不像SimplePie那样专门关注提要,所以它不是一个真正的替代方案,它只适用于所讨论的提要。非常感谢您的帮助,我使用的是今天下载的1.2.1。这就是我为每个feed(比如stackoverflow.com/feed等)编写的代码。。除了谷歌新闻提要。我很困惑,我相信我已经找到了问题所在。这是我的托管公司[Rackspace]的问题。当我在另一家公司的另一个托管帐户上试用时,它的效果非常好。谢谢@Ryan前来帮忙。小心点,我也面临着同样的问题。你能告诉我你的主持人到底有什么问题吗?是的,请告诉我们你的主持人有什么问题吗?上面的答案毫无意义,因为答案可以按日期、投票或活动排序。请使用答案的链接或使用答案的作者姓名。