Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
如何制作rss源_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 !
好的,下载示例文件并开始编码!
Ok, here are all used PHP files:
好的,这是所有使用过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); ?>
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; } ?>
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.
这是我们的数据库类。 无需在此处提供该文件的完整代码(足够大)。 它总是作为下载包提供。
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>"; } } ?>
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');
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
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章