0

我需要从 12 个不同的网站获取 RSS 提要。我只想在主页上向用户显示选定的提要。我正在使用 Magpie RSS 解析器来显示 RSS 提要。但是我如何将它们存储在数据库中。我使用以下代码来获取提要。

<?php
include('magpierss/rss_fetch.inc');
define('MAGPIE_CACHE_DIR', '/var/cache');
$rss = fetch_rss(' here is url link');
$items = array_slice($rss->items, 0, 10);

foreach ($items as $item) {
    $href = $item['link'];
    $title = $item['title'];
    $desc = $item['description'];
    echo "<p><a href='$href'>$title</a><br>";
    if ($desc) {
        if (strlen($desc) >= 125) {
            $desc = substr($desc, 0, 124) . "...";
        }
    }
    echo $desc;
}
;
?>

现在我如何将它存储到数据库中。

4

1 回答 1

0

试试这个逻辑,我希望它对你有用......

<?php
include('magpierss/rss_fetch.inc');
define('MAGPIE_CACHE_DIR', '/var/cache');
$rss = fetch_rss(' here is url link');
$items = array_slice($rss->items, 0, 10);

$data = array();
$i = 0;
foreach ($items as $item) {
        $href = $item['link'];
        $title = $item['title'];
        $desc = $item['description'];
        echo "<p><a href='$href'>$title</a><br>";
        if($desc)
        if (strlen($desc) >= 125)
        {
                $desc = substr($desc,0,124)."...";
        }
        echo $desc;

        $data[$i]['href'] = $href;
        $data[$i]['title'] = $title;
        $data[$i]['desc'] = $desc;

        //create insert function like this InsertDbEntry($data) and set in function $data as argument;

$i++;
}
;

function InsertDbEntry($data){
    // Also you can add if condition for check this title is already exists in database or not
    foreach($data as $dbData){
        mysql_query("insert into table ('href','title','desc') values ('".$dbData['href']."','".$dbData['title']."','".$dbData['desc']."')");
    }
} 
于 2012-06-26T09:09:53.677 回答