2011-11-09 61 views
-1

我想從此rss訂閱源讀取並保存一些數據到我的數據庫表中。 RSS訂閱是http://feeds.feedburner.com/TechCrunch/獲取來自複雜rss訂閱源的所有數據

我用下面的代碼之前讀取另一個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提要中獲取這些數據?請,我需要指導。

+0

您已添加沒有關於您的數據庫(MySQL,MSSQL,Oracle)的信息,並且您已經擁有數據庫。請提供更多信息。 – Manuel

+0

我正在使用mysql,我沒有任何問題插入我的數據庫,但我想知道我是如何從rss提要讀取這些數據的。 – omnath

+0

dragon112,從代碼中可以清楚的看出,他已經擁有數據庫連接,並且基礎數據庫和他正在使用MySQL。 –

回答

1

當您在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()提取。

+0

我使用您的代碼和它的工作標題,鏈接,說明,但圖像仍然無法正常工作。它保存在描述列中,但即使使用「strip_tags()」我也不能保存它。 – Arif