从一个复杂的RSS提要获取所有数据


get the all data from a complex rss feed

我想从这个rss提要中读取并保存一些数据到我的数据库表中。的RSS提要是http://feeds.feedburner.com/TechCrunch/。

我以前使用过下面的代码来读取另一个RSS提要:

$homepage = file_get_contents('http://rss.cnn.com/rss/edition_technology.rss');
$homepage = preg_replace("/(<'/?)('w+):([^>]*>)/", "$1$2$3", $homepage);
$xml = simplexml_load_string($homepage,'SimpleXMLElement', LIBXML_NOCDATA);
echo '<pre>';
print_r($xml);
foreach($xml->channel->item as $opt) {
    $title = mysql_real_escape_string($opt->title);
    $link = mysql_real_escape_string($opt->link);
    $des = mysql_real_escape_string($opt->description);
    // and others
    $sql = 
        "INSERT INTO store_feed (title, link, description) 
         VALUES('$title','$link','$des') 
         ON DUPLICATE KEY UPDATE title = '$title', description = '$des'";
    $result = mysql_query($sql) or die( mysql_error() );
}

…我得到了想要的数据,但这次的数据不同。

我想存储链接,描述,图像,发布日期,这个提要的标题。我该怎么做呢?

我知道如何插入到数据库中,但我如何从RSS提要获得此数据?拜托,我需要指导。

当您在xml字符串上使用simplexml_load_string()时,您将其转换为对象树。

这个XML:

<channel>
  <item>
    <title>Example Title</title>
    <description>Example Description</description>
  </item>
</channel>

…被转换成可以这样使用的格式:

$xml->channel->item->title;
$xml->channel->item->description;

因此,您需要查看新RSS提要的XML,以了解如何更改代码。它可能看起来像这样:

foreach($xml->channel->item as $opt) {
    $title = mysql_real_escape_string($opt->title);
    $link = mysql_real_escape_string($opt->link);
    $des = mysql_real_escape_string($opt->description);
    $publication_date = mysql_real_escape_string($opt->pubDate);
    $image = mysql_real_escape_string(strip_tags($opt->description, '<img>'));
}

图像位于描述中,因此我们可以使用strip_tags()提取它。