您的位置:首页 > Web前端

搜索或添加rss feed_如何轻松制作自己的RSS Feed

2020-08-06 10:10 134 查看

搜索或添加rss feed

How to Easily Make your own RSS feed using PHP + SQL

如何使用PHP + SQL轻松制作自己的RSS feed

RSS Feed is an important part of a website/blog to attract and bring back users and make them permanent readers. Blogs like wordpress have built in rss reader, but if your website doesn’t have a rss system this tutorial is just the thing you need.  As we know, RSS feeds usually using to transfer some news to readers. Today I will tell you how to create such feeds and fill it with information from database. Here is a sample:

RSS Feed是网站/博客的重要组成部分,可以吸引并带回用户,并使他们成为永久读者。 诸如wordpress之类的博客已经内置了rss阅读器,但是如果您的网站没有rss系统,那么本教程正是您所需要的。 众所周知,RSS提要通常用于将一些新闻传递给读者。 今天,我将告诉您如何创建这样的提要,并用数据库中的信息填充它。 这是一个示例:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Ok, download the example files and lets start coding !

好的,下载示例文件并开始编码!

步骤1. PHP (Step 1. PHP)

Ok, here are all used PHP files:

好的,这是所有使用过PHP文件:

index.php (index.php)

This file will generate RSS feed

该文件将生成RSS feed

<?php
require_once('inc/db.inc.php');
require_once('inc/rss_factory.inc.php');
$sSiteUrl = 'http://localhost/dolphin7_rep_svn/demos/script-tutorials.com/9/';
$sRssIcon = 'https://www.script-tutorials.com/logo-tuts.png';
$aStoriesRSS = array();
$sSQL = "SELECT * FROM `stories` ORDER BY `id` DESC";
$aStories = $GLOBALS['MySQL']->getAll($sSQL);
foreach ($aStories as $iID => $aStoryInfo) {
$iStoryID = (int)$aStoryInfo['id'];
$aStoriesRSS[$iID]['Guid'] = $iStoryID;
$aStoriesRSS[$iID]['Title'] = $aStoryInfo['title'];
$aStoriesRSS[$iID]['Link'] = $sSiteUrl . 'view.php?id=' . $iStoryID;
$aStoriesRSS[$iID]['Desc'] = $aStoryInfo['description'];
$aStoriesRSS[$iID]['DateTime'] = $aStoryInfo['when'];
}
$oRssFactory = new RssFactory();
header('Content-Type: text/xml; charset=utf-8');
echo $oRssFactory->GenRssByData($aStoriesRSS, 'Our stories', $sSiteUrl . 'index.php', $sRssIcon);
?>
<?php
require_once('inc/db.inc.php');
require_once('inc/rss_factory.inc.php');
$sSiteUrl = 'http://localhost/dolphin7_rep_svn/demos/script-tutorials.com/9/';
$sRssIcon = 'https://www.script-tutorials.com/logo-tuts.png';
$aStoriesRSS = array();
$sSQL = "SELECT * FROM `stories` ORDER BY `id` DESC";
$aStories = $GLOBALS['MySQL']->getAll($sSQL);
foreach ($aStories as $iID => $aStoryInfo) {
$iStoryID = (int)$aStoryInfo['id'];
$aStoriesRSS[$iID]['Guid'] = $iStoryID;
$aStoriesRSS[$iID]['Title'] = $aStoryInfo['title'];
$aStoriesRSS[$iID]['Link'] = $sSiteUrl . 'view.php?id=' . $iStoryID;
$aStoriesRSS[$iID]['Desc'] = $aStoryInfo['description'];
$aStoriesRSS[$iID]['DateTime'] = $aStoryInfo['when'];
}
$oRssFactory = new RssFactory();
header('Content-Type: text/xml; charset=utf-8');
echo $oRssFactory->GenRssByData($aStoriesRSS, 'Our stories', $sSiteUrl . 'index.php', $sRssIcon);
?>
[/code]

view.php (view.php)

We will draw post page using this page

我们将使用此页面绘制帖子页面

<?php
require_once('inc/db.inc.php');
$iStoryID = (int)$_GET['id'];
if ($iStoryID > 0) {
$aStoryInfo = $GLOBALS['MySQL']->getRow("SELECT * FROM `stories` WHERE `id`='{$iStoryID}'");
$sStoryTitle = $aStoryInfo['title'];
$sStoryDesc = $aStoryInfo['description'];
echo <<<EOF
<h1>{$sStoryTitle}</h1>
<div>{$sStoryDesc}</div>
<hr />
<div><a href="index.php">Back to RSS</a></div>
EOF;
}
?>
<?php
require_once('inc/db.inc.php');
$iStoryID = (int)$_GET['id'];
if ($iStoryID > 0) {
$aStoryInfo = $GLOBALS['MySQL']->getRow("SELECT * FROM `stories` WHERE `id`='{$iStoryID}'");
$sStoryTitle = $aStoryInfo['title'];
$sStoryDesc = $aStoryInfo['description'];
echo <<<EOF
<h1>{$sStoryTitle}</h1>
<div>{$sStoryDesc}</div>
<hr />
<div><a href="index.php">Back to RSS</a></div>
EOF;
}
?>
[/code]

inc / db.inc.php (inc/db.inc.php)

This is our database class. No need to give full code of that file here (it big enough). It always available as a download package.

这是我们的数据库类。 无需在此处提供该文件的完整代码(足够大)。 它总是作为下载包提供。

inc / rss_factory.inc.php (inc/rss_factory.inc.php)

This is our RSS factory class. Universal class which transforming array with information (in necessary format) into RSS code.

这是我们的RSS工厂类。 通用类,它将带有信息的数组(必要格式)转换为RSS代码。

<?php
class RssFactory {
// constructor
function RssFactory() {}
// rss generator
function GenRssByData($aRssData, $sTitle, $sMainLink, $sImage = '') {
$sRSSLast = '';
if (isset($aRssData[0]))
$sRSSLast = $aRssData[0]['DateTime'];
$sUnitRSSFeed = '';
foreach ($aRssData as $iUnitID => $aUnitInfo) {
$sUnitUrl = $aUnitInfo['Link'];
$sUnitGuid = $aUnitInfo['Guid'];
$sUnitTitle = $aUnitInfo['Title'];
$sUnitDate = $aUnitInfo['DateTime'];
$sUnitDesc = $aUnitInfo['Desc'];
$sUnitRSSFeed .= "<item><title><![CDATA[{$sUnitTitle}]]></title><link><![CDATA[{$sUnitUrl}]]></link><guid><![CDATA[{$sUnitGuid}]]></guid><description><![CDATA[{$sUnitDesc}]]></description><pubDate>{$sUnitDate}</pubDate></item>";
}
$sRSSTitle = "{$sTitle} RSS";
$sRSSImage = ($sImage != '') ? "<image><url>{$sImage}</url><title>{$sRSSTitle}</title><link>{$sMainLink}</link></image>" : '';
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel><title>{$sRSSTitle}</title><link>{$sMainLink}</link><description>{$sRSSTitle}</description><lastBuildDate>{$sRSSLast}</lastBuildDate>{$sRSSImage}{$sUnitRSSFeed}</channel></rss>";
}
}
?>
<?php
class RssFactory {
// constructor
function RssFactory() {}
// rss generator
function GenRssByData($aRssData, $sTitle, $sMainLink, $sImage = '') {
$sRSSLast = '';
if (isset($aRssData[0]))
$sRSSLast = $aRssData[0]['DateTime'];
$sUnitRSSFeed = '';
foreach ($aRssData as $iUnitID => $aUnitInfo) {
$sUnitUrl = $aUnitInfo['Link'];
$sUnitGuid = $aUnitInfo['Guid'];
$sUnitTitle = $aUnitInfo['Title'];
$sUnitDate = $aUnitInfo['DateTime'];
$sUnitDesc = $aUnitInfo['Desc'];
$sUnitRSSFeed .= "<item><title><![CDATA[{$sUnitTitle}]]></title><link><![CDATA[{$sUnitUrl}]]></link><guid><![CDATA[{$sUnitGuid}]]></guid><description><![CDATA[{$sUnitDesc}]]></description><pubDate>{$sUnitDate}</pubDate></item>";
}
$sRSSTitle = "{$sTitle} RSS";
$sRSSImage = ($sImage != '') ? "<image><url>{$sImage}</url><title>{$sRSSTitle}</title><link>{$sMainLink}</link></image>" : '';
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"><channel><title>{$sRSSTitle}</title><link>{$sMainLink}</link><description>{$sRSSTitle}</description><lastBuildDate>{$sRSSLast}</lastBuildDate>{$sRSSImage}{$sUnitRSSFeed}</channel></rss>";
}
}
?>
[/code]

步骤3. SQL (Step 3. SQL)

We will need to execute next SQL in our database. We will create table with demo stories which going to show in RSS

我们将需要在数据库中执行下一个SQL。 我们将创建带有演示故事的表,这些故事将在RSS中显示

CREATE TABLE `stories` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`when` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `stories` (`id`, `title`, `description`, `when`) VALUES
(1, 'First story', 'First story description here', '2010-06-01 00:00:00'),
(2, 'Second story', 'Second story description here', '2010-06-02 00:00:00'),
(3, 'Third story', 'Third story description here', '2010-06-03 00:00:00'),
(4, 'Fourth story', 'Fourth story description here', '2010-06-04 00:00:00'),
(5, 'Fifth story', 'Fifth story description here', '2010-06-05 00:00:00'),
(6, 'Sixth story', 'Sixth story description here', '2010-06-06 00:00:00'),
(7, 'Seventh story', 'Seventh story description here', '2010-06-07 00:00:00'),
(8, 'Eighth story', 'Eighth story description here', '2010-06-08 00:00:00'),
(9, 'Ninth story', 'Ninth story description here', '2010-06-09 00:00:00'),
(10, 'Tenth story', 'Tenth story description here', '2010-06-10 00:00:00');
CREATE TABLE `stories` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`when` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `stories` (`id`, `title`, `description`, `when`) VALUES
(1, 'First story', 'First story description here', '2010-06-01 00:00:00'),
(2, 'Second story', 'Second story description here', '2010-06-02 00:00:00'),
(3, 'Third story', 'Third story description here', '2010-06-03 00:00:00'),
(4, 'Fourth story', 'Fourth story description here', '2010-06-04 00:00:00'),
(5, 'Fifth story', 'Fifth story description here', '2010-06-05 00:00:00'),
(6, 'Sixth story', 'Sixth story description here', '2010-06-06 00:00:00'),
(7, 'Seventh story', 'Seventh story description here', '2010-06-07 00:00:00'),
(8, 'Eighth story', 'Eighth story description here', '2010-06-08 00:00:00'),
(9, 'Ninth story', 'Ninth story description here', '2010-06-09 00:00:00'),
(10, 'Tenth story', 'Tenth story description here', '2010-06-10 00:00:00');
[/code]
现场演示

结论 (Conclusion)

Today I told you how to create own RSS feed. Hope all my code easy to understand and comfortable to use. You can use this material to create own scripts into your startups. Good luck!

今天,我告诉您如何创建自己的RSS提要。 希望我所有的代码都易于理解和使用。 您可以使用此材料在创业公司中创建自己的脚本。 祝好运!

翻译自: https://www.script-tutorials.com/how-to-easily-make-your-own-rss-feed/

搜索或添加rss feed

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: