【问题标题】:how to store rss in mysql with Magpie in php如何在 mysql 中使用 Magpie 在 php 中存储 rss
【发布时间】:2012-06-26 08:54:25
【问题描述】:

我需要从 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;
}
;
?>

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

【问题讨论】:

  • 无法理解存储的哪一部分对您有问题。你能写出你尝试过的东西吗?您在 DB 中有哪些表?
  • 您可以创建简单的查询,但主要问题是:“这是个好主意吗?”
  • @PLB 我们可以根据客户要求做什么?他们要求这个东西将所有提要存储在数据库中,然后通过选择将它们发布到前端。
  • @lvil 非常适合数据库,我需要遵循字段demo_feeds( Published` datetime DEFAULT NULL, Author varchar(25) CHARACTER SET latin1 DEFAULT NULL, Title mediumtext COLLATE utf8_unicode_ci, Content longtext COLLATE utf8_unicode_ci, Link mediumtext CHARACTER SET latin1, Categories mediumtext CHARACTER SET latin1, itemid varchar(40) CHARACTER SET latin1 DEFAULT NULL, Id int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (@987530@) , 唯一键 Id (Id) ) ;

标签: php mysql magpie


【解决方案1】:

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

<?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']."')");
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2011-10-23
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多