3

我一直在关注我之前的一篇文章(通过 php 在 Intranet 上显示 RSS 提要项目)的回复,并尝试训练自己。

我现在正在检索数据,但是如何获得“完整的帖子/故事”,我似乎只能获得标题和描述:

<?php
$a = curl_init("http://newsrss.bbc.co.uk/rss/on_this_day/front_page/rss.xml");
curl_setopt($a, CURLOPT_RETURNTRANSFER,1);
$b = curl_exec($a);
$xml = simplexml_load_string($b);
//var_dump($xml);
print "<pre>";
    print_r($xml);
    print "</pre>";
?>

这是我的输出:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => BBC On This Day | Front Page
            [link] => http://news.bbc.co.uk/go/rss/-/onthisday/default.stm
            [description] => SimpleXMLElement Object
                (
                )

            [language] => en-gb
            [lastBuildDate] => Mon, 25 Oct 2010 00:15:09 GMT
            [copyright] => Copyright: (C) British Broadcasting Corporation, http://news.bbc.co.uk/2/shared/bsp/hi/services/copyright/html/default.stm
            [docs] => http://www.bbc.co.uk/syndication/
            [ttl] => 15
            [image] => SimpleXMLElement Object
                (
                    [title] => BBC On This Day
                    [url] => http://news.bbc.co.uk/nol/shared/img/bbc_news_120x60.gif
                    [link] => http://news.bbc.co.uk/go/rss/-/onthisday/default.stm
                )

            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => 1983: US troops invade Grenada
                            [description] => American forces seize control of the Caribbean island of Grenada less than a week after a left-wing coup in which the prime minister, Maurice Bishop, was killed.
                            [link] => http://news.bbc.co.uk/go/rss/-/onthisday/hi/dates/stories/october/25/newsid_3207000/3207509.stm
                            [guid] => http://news.bbc.co.uk/onthisday/hi/dates/stories/october/25/newsid_3207000/3207509.stm
                            [pubDate] => Fri, 24 Oct 2003 08:35:04 GMT
                            [category] => 25
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [title] => 1964: President Kaunda takes power in Zambia
                            [description] => Zambia is the ninth African state to gain independence from the British crown.
                            [link] => http://news.bbc.co.uk/go/rss/-/onthisday/hi/dates/stories/october/25/newsid_2658000/2658325.stm
                            [guid] => http://news.bbc.co.uk/onthisday/hi/dates/stories/october/25/newsid_2658000/2658325.stm
                            [pubDate] => Thu, 06 Feb 2003 16:40:25 GMT
                            [category] => 25
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [title] => 1984: Europe grants emergency aid for Ethiopia
                            [description] => The EEC is donating £1.8 million to help combat the famine in Ethiopia.
                            [link] => http://news.bbc.co.uk/go/rss/-/onthisday/hi/dates/stories/october/25/newsid_2478000/2478431.stm
                            [guid] => http://news.bbc.co.uk/onthisday/hi/dates/stories/october/25/newsid_2478000/2478431.stm
                            [pubDate] => Thu, 14 Nov 2002 17:55:28 GMT
                            [category] => 25
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [title] => 1976: Queen opens National Theatre in London
                            [description] => The Queen has officially opened the National Theatre on the South Bank in London after years of delays.
                            [link] => http://news.bbc.co.uk/go/rss/-/onthisday/hi/dates/stories/october/25/newsid_2478000/2478397.stm
                            [guid] => http://news.bbc.co.uk/onthisday/hi/dates/stories/october/25/newsid_2478000/2478397.stm
                            [pubDate] => Thu, 14 Nov 2002 17:45:57 GMT
                            [category] => 25
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [title] => 2001: Crime rates lowest for 20 years
                            [description] => British Crime Survey reveals the chances of being a victim of crime are lowest for 20 years.
                            [link] => http://news.bbc.co.uk/go/rss/-/onthisday/hi/dates/stories/october/25/newsid_2478000/2478379.stm
                            [guid] => http://news.bbc.co.uk/onthisday/hi/dates/stories/october/25/newsid_2478000/2478379.stm
                            [pubDate] => Thu, 14 Nov 2002 17:42:23 GMT
                            [category] => 25
                        )

                )

        )

)
4

2 回答 2

0

试试这个代码:

<?php
require_once 'simple_html_dom.php';

$a = curl_init("http://newsrss.bbc.co.uk/rss/on_this_day/front_page/rss.xml");
curl_setopt($a, CURLOPT_RETURNTRANSFER,1);
$b = curl_exec($a);
$xml = simplexml_load_string($b);
$items = $xml->xpath('//channel/item');

foreach ($items as $item) {
    var_dump((string)$item->guid);
    $a = curl_init((string)$item->guid);
    curl_setopt($a, CURLOPT_RETURNTRANSFER,1);
    $body = curl_exec($a);
    $html = str_get_html($body);
    $newsContent = $html->find('div[class=bodytext]');
    $newsContent = $newsContent[0];
    print "<pre>";
    print_r($newsContent->innertext);
    print "</pre>";
}

你也需要这个库来存放 php 代码。

于 2012-06-06T06:44:18.597 回答
0

您可以尝试像下面这样的 Domdocument 来阅读 rss 提要标签

$xmlDoc = new DOMDocument();

$xmlDoc->load($a);

$x=$xmlDoc->getElementsByTagName('entry');
于 2012-07-19T11:45:40.173 回答