2011-12-29 114 views
2

此代碼完全適用於任何其他rss飼料,但不適用於谷歌新聞飼料。我不知道我在做什麼錯,我認爲這是一些錯誤。 當我嘗試閱讀谷歌新聞我不斷收到此錯誤飼料simplepie不解析谷歌新聞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> 

+1

我相信我發現了這個問題。這是我的託管公司[Rackspace]的問題。當我在另一家公司的另一個託管帳戶上嘗試它時,它的工作就像一個魅力。感謝@Ryan來幫忙。保重 – acrobat 2011-12-30 01:19:00

+1

我面臨着幾乎相同的問題。你能告訴我你的主機問題到底是什麼嗎? – 2012-07-07 18:00:11

+0

是的,請告訴我們您的主機有什麼問題? – PinoyStackOverflower 2012-11-22 06:40:18

回答

4

確保您使用SimplePie 1.2.1,1.2曾與URL解析錯誤這可能會導致這種類型的錯誤。

(我也是領先了SimplePie開發者,可以隨意齊刷刷地到我的郵箱問題)如果你正在使用1.2.1

,這樣看來,這是bug #162體現這是目前未經證實。我將深入研究這一點,但它看起來絕對是SimplePie中的錯誤,而不是代碼中的錯誤。

(我還會回到這裏後與這是爲什麼發生的好奇之中你。)

+0

非常感謝您的幫助,我今天使用1.2.1下載。這是我已經爲每個飼料,如「stackoverflow.com/feed」等工作的代碼,除了谷歌新聞提要。我感到困惑。 – acrobat 2011-12-29 05:02:50

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

+0

謝謝哈克雷,我試過你的腳本在我的服務器上,它似乎無法正常工作。有一些要求嗎? – acrobat 2011-12-29 04:36:26

+0

只要您知道提要符合RSS/Atom,SimpleXML就可以。問題在於,互聯網上的大部分Feed都沒有。 SimplePie其實就是SimpleXML上的規範化層。 – 2011-12-29 04:38:38

+0

@Ryan McCue:SimpleXML並沒有像SimplePie那樣專注於Feed,所以它不是真正的替代品,它只是用於Feed的問題。 – hakre 2011-12-29 04:41:23

0

對於谷歌新聞飼料用途:

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

只是想在此處添加註釋爲他人認爲上述答案不起作用。如果您在項目標題上獲得空白,請檢查Feed源,您的simplepie或腳本可能沒有任何問題,但由於標題項目標記中的html代碼,您的瀏覽器將其設置爲空。

+0

「以上答案」是無意義的,因爲答案可以按日期,投票或活動排序。請使用答案的鏈接或使用答案的作者姓名。 – Moritz 2018-01-05 21:17:55