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

搜索或添加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);
?>

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;
}
?>

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>";
	}
}
?>

步骤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');

现场演示

结论 (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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用SAX解析器实现RSS Feed文件的解析,可以按照以下步骤进行: 1. 创建SAXParserFactory和SAXParser对象:首先需要创建SAXParserFactory对象和SAXParser对象,使用它们来解析RSS Feed文件。 ```java SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); ``` 2. 创建RSSFeedHandler类:创建一个RSSFeedHandler类,继承自DefaultHandler类,用于处理SAX解析器解析XML文件时的事件。 ```java public class RSSFeedHandler extends DefaultHandler { // 处理startElement事件 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // ... } // 处理endElement事件 @Override public void endElement(String uri, String localName, String qName) throws SAXException { // ... } // 处理characters事件 @Override public void characters(char[] ch, int start, int length) throws SAXException { // ... } } ``` 3. 实现RSSFeedHandler类的方法:在RSSFeedHandler类中实现startElement、endElement和characters等方法,用于处理XML文件中的元素和内容。比如,在startElement方法中,可以判断当前元素是否是item元素,如果是,则创建一个RSSItem对象,并将其添加RSS Feed中。 ```java public class RSSFeedHandler extends DefaultHandler { private RSSFeed feed; private RSSItem item; // 处理startElement事件 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("item")) { item = new RSSItem(); feed.addItem(item); } else if (qName.equalsIgnoreCase("title") && item != null) { item.setTitle(true); } else if (qName.equalsIgnoreCase("link") && item != null) { item.setLink(true); } else if (qName.equalsIgnoreCase("description") && item != null) { item.setDescription(true); } } // 处理endElement事件 @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("item")) { item = null; } } // 处理characters事件 @Override public void characters(char[] ch, int start, int length) throws SAXException { if (item != null) { item.addContent(new String(ch, start, length)); } } } ``` 4. 解析RSS Feed文件:使用SAXParser对象的parse方法,解析RSS Feed文件,并将RSS Feed对象返回。 ```java public RSSFeed parse(String url) throws Exception { RSSFeedHandler handler = new RSSFeedHandler(); parser.parse(url, handler); return handler.getFeed(); } ``` 总体来说,使用SAX解析器实现RSS Feed文件的解析,需要创建SAXParserFactory对象和SAXParser对象,实现RSSFeedHandler类的方法,并使用SAXParser对象的parse方法解析RSS Feed文件。SAX解析器是一种基于事件驱动的解析器,相比DOM解析器,它更加轻量级,适合处理大型XML文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值