2

このコードは、他のRSSフィードでは完全に機能しますが、Googleニュースフィードでは機能しません。何が間違っているのかわかりません。バグだと思います。Googleニュースフィードを読み込もうとすると、このエラーが発生し続けます

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フィードを試してみるとうまく機能しますが、Googleニュースフィードでは機能しません。誰かが私にヒントを与えることができますか?

<?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を使用している場合、これは現在確認されていないバグ#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

上記の答えがうまくいかないと思う他の人のために、ここにメモを追加したかっただけです。アイテムのタイトルがnullになる場合は、フィードソースを確認してください。シンプルなスクリプトやスクリプトに問題はない可能性がありますが、タイトルのアイテムタグ内のhtmlコードが原因で、ブラウザがnullに設定しています。

于 2018-01-05T21:00:21.827 に答える