使用simplexml将多个rss feed插入MYSQL

时间:2012-04-22 14:54:17

标签: php mysql xml rss simplexml

下面是我的代码,用于将多个rss提要解析为mysql数据库。 我认为在foreach部分我做错了,因为没有输出。 然而,数据库被填满了。使用1个feed时,脚本运行正常。 有谁看到我做错了什么?非常感谢提前:)

$feeds = ('https://www.ictu.nl/rss.xml', 'http://www.vng.nl/smartsite.dws?id=97817');
$xml = simplexml_load_file($feeds);

foreach($xml->channel->item as $item)
{
$date_format = "j-n-Y"; // 7-7-2008
echo date($date_format,strtotime($item->pubDate));  
         echo '<a href="'.$item->link.'" target="_blank">'.$item->title.'</a>';
         echo '<div>' . $item->description . '</div>';

mysql_query("INSERT INTO rss_feeds (id, title, description, link, pubdate) 
VALUES (
    '', 
    '".mysql_real_escape_string($item->title)."', 
    '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
    '".mysql_real_escape_string($item->link)."', 
    '".mysql_real_escape_string($item->pubdate)."')");       
}

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php
$feeds = array('https://www.ictu.nl/rss.xml', 'http://www.vng.nl/smartsite.dws?id=97817');
foreach( $feeds as $feed ) {
    $xml = simplexml_load_file($feed);

    foreach($xml->channel->item as $item)
    {
    $date_format = "j-n-Y"; // 7-7-2008
    echo date($date_format,strtotime($item->pubDate));  
             echo '<a href="'.$item->link.'" target="_blank">'.$item->title.'</a>';
             echo '<div>' . $item->description . '</div>';

    mysql_query("INSERT INTO rss_feeds (id, title, description, link, pubdate) 
    VALUES (
        '', 
        '".mysql_real_escape_string($item->title)."', 
        '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
        '".mysql_real_escape_string($item->link)."', 
        '".mysql_real_escape_string($item->pubdate)."')");       
    }
}
?>

希望它有所帮助。