2011-12-29 03:35
浏览 50

simplepie没有解析google news rss feed

This code works perfectly with any other rss feed but not with google news feeds. I do not know what I am doing wrong, I think it's some bug. I keep getting this error when I try to read google news feeds

This XML document is invalid, likely due to invalid characters. XML error: SYSTEM or PUBLIC, the URI is missing at line 1, column 61

For example if we try the http://stackoverflow.com/feeds feeds it works nicely, but not with google news feeds. Can some one give me a hint?


    //get the simplepie library

    //grab the feed
    $feed = new SimplePie();


    //enable caching

    //provide the caching folder

    //set the amount of seconds you want to cache the feed

    //init the process

    //let simplepie handle the content type (atom, RSS...)


<!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">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<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>


    <?php endforeach; ?>


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dpvhv66448 2011-12-29 04:37

    Make sure you're using SimplePie 1.2.1, 1.2 had a bug with URL parsing which can cause this type of error.

    (I'm also the SimplePie lead developer, so feel free to shoot questions straight to my email)

    If you are using 1.2.1, it would appear that this is a manifestation of bug #162 which is currently unconfirmed. I'll take an indepth look into this, but it appears to definitely be an error in SimplePie, not in your code.

    (I'll also post back here with why this is occurring for the curious amongst you.)

    点赞 评论
  • dqunzip3183 2011-12-29 04:14

    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, "
    <", $feed->channel->link, ">
    foreach($feed->channel->item as $item)
        echo "* $item->title

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


    点赞 评论
  • drzablspw01655860 2016-09-07 20:18

    For Google News feed uses :

    点赞 评论
  • duanqiao9541 2018-01-05 21:00

    Just wanted to add a note here for others that think the above answer doesn't work. If your getting a null on item title, check the feed source, it may not be anything wrong with your simplepie or script, but your browser setting it to null because of html code within the title item tags.

    点赞 评论

相关推荐 更多相似问题