It is very easy to generate RSS simply by using the rss format and then generating the output to the browser. The point is to tell the browser that you are using the rss format data. For details, refer to an example. It is very easy to generate RSS simply by using the rss format and then generating the output to the browser. The point is to tell the browser that you are using the rss format data. For details, refer to an example.
Script ec (2); script
Rss (simple information aggregation is also called aggregate content) is a format for describing and synchronizing website content. The following code generates an RSS subscription:
Rss XML structure
Website name
Http://www.111cn.net/
Website description!
RSS Tutorial
Website address/rss
New RSS tutorial on W3School
XML Tutorial
Website address/xml
New XML tutorial on W3School
RSS instance
'. $ WebName .'
'. $ WebUrl .'
'. $ WebDesc .'
'. $ This-> createItem ().'
'; Echo $ html;} private function createItem () {// RSS item // $ data can be replaced with your own data $ html = ''; // Article data $ data = array ('id' => 1, 'date' => date ('R', time ()), 'title' => 'Article title', 'link' => 'HTTP: // www.111cn.net ', // Article address 'description' => 'website description '); for ($ I = 0; $ I <6; $ I ++) {$ html. ='
'. $ Data ['title'].'
'. $ Data ['link'].'
'. $ Data ['date'].'
'.$data['description'].'
';} Return $ html;} header ("Content-Type: text/xml; charset = UTF-8"); $ rss = new Rss (); $ rss-> createFeed (); exit;?>
After an RSS Feed is generated, how can I add RSS to a website? And let Firefox, IE7 or other Feed robots automatically discover? Simply add a specific Link label in the Head section of the webpage, as shown below:
Set title to Feed title, href to Feed address, and everything will be OK!
Example 2
Php programmer tutorial Network
Http://www.111cn.net/
This site is a php programmer's Work and Life notes!
RSS Tutorial
Website address/rss
New RSS tutorial on W3School
XML Tutorial
Website address/xml
New XML tutorial on W3School
The following is a sample code for dynamically generating RSS using php:
Description); $ descriptionField-> syndicateHtml = $ this-> descriptionHtmlSyndicated; $ descriptionField-> truncSize = $ this-> descriptionTruncSize; return $ descriptionField-> output ();}} class FeedHtmlField {var $ rawFieldContent; var $ truncSize, $ syndicateHtml; function FeedHtmlField ($ parFieldContent) {if ($ parFieldContent) {$ this-> rawFieldContent = $ parFieldContent ;}} function output () {if (! $ This-> rawFieldContent) {$ result = "";} elseif ($ this-> syndicateHtml) {$ result ="".$this->rawFieldContent."";}Else {if ($ this-> truncSize and is_int ($ this-> truncSize) {$ result = FeedCreator: iTrunc (htmlspecialchars ($ this-> rawFieldContent ), $ this-> truncSize);} else {$ result = htmlspecialchars ($ this-> rawFieldContent);} return $ result;} class UniversalFeedCreator extends FeedCreator {var $ _ feed; function _ setFormat ($ format) {switch (strtoupper ($ format) {case "2.0": // fall through case "RSS2.0": $ this-> _ fe Ed = new RSSCreator20 (); break; case "0.91": // fall through case "RSS0.91": $ this-> _ feed = new RSSCreator091 (); break; default: $ this-> _ feed = new RSSCreator091 (); break;} $ vars = get_object_vars ($ this); foreach ($ vars as $ key => $ value) {// prevent overwriting of properties "contentType", "encoding"; do not copy "_ feed" itself if (! In_array ($ key, array ("_ feed", "contentType", "encoding "))) {$ this-> _ feed->{$ key }=$ this-> {$ key };}} function createFeed ($ format = "RSS0.91 ") {$ this-> _ setFormat ($ format); return $ this-> _ feed-> createFeed ();} function saveFeed ($ format = "RSS0.91 ", $ filename = "", $ displayContents = true) {$ this-> _ setFormat ($ format); $ this-> _ feed-> saveFeed ($ filename, $ displayContents);} function useCached ($ format = "RSS0.91", $ filename = "", $ timeout = 3600) {$ this-> _ setFormat ($ format ); $ this-> _ feed-> useCached ($ filename, $ timeout) ;}} class FeedCreator extends HtmlDescribable {var $ title, $ description, $ link; var $ syndicationURL, $ image, $ language, $ copyright, $ pubDate, $ lastBuildDate, $ editor, $ editorEmail, $ webmaster, $ category, $ docs, $ ttl, $ rating, $ skipHours, $ skipDays; var $ javasstylesheet = ""; var $ items = Array (); var $ contentType = "application/xml"; var $ encoding = "UTF-8 "; var $ additionalElements = Array (); function addItem ($ item) {$ this-> items [] = $ item;} function clearItem2Null () {$ this-> items = array ();} function iTrunc ($ string, $ length) {if (strlen ($ string) <= $ length) {return $ string ;} $ pos = strrpos ($ string ,". "); if ($ pos >=$ length-4) {$ string = substr ($ string, 0, $ length-4); $ pos = strrpos ($ string, ". ");} if ($ pos >=$ length * 0.4) {return substr ($ string, 0, $ pos + 1 ). "... ";}$ pos = strrpos ($ string," "); if ($ pos >=$ length-4) {$ string = substr ($ string, 0, $ length-4); $ pos = strrpos ($ string, "");} if ($ pos >=$ length * 0.4) {return substr ($ string, 0, $ pos ). "... ";} return substr ($ string, 0, $ length-4 ). "... ";} function _ createGeneratorComment () {return"
\ N ";} function _ createAdditionalElements ($ elements, $ indentString =" ") {$ AE =" "; if (is_array ($ elements )) {foreach ($ elements AS $ key => $ value) {$ AE. = $ indentString. "<$ key> $ value
\ N ";}}return $ AE;} function _ createStylesheetReferences () {$ xml =" "; if ($ this-> cssStyleSheet) $ xml. ="
CssStyleSheet. "\" type = \ "text/css \"?> \ N "; if ($ this-> export stylesheet) $ xml. ="
XslStyleSheet. "\" type = \ "text/xsl \"?> \ N "; return $ xml;} function createFeed () {} function _ generateFilename () {$ fileInfo = pathinfo ($ _ SERVER [" PHP_SELF "]); return substr ($ fileInfo ["basename"], 0,-(strlen ($ fileInfo ["extension"]) + 1 )). ". xml ";} function _ redirect ($ filename) {Header (" Content-Type :". $ this-> contentType. "; charset = ". $ this-> encoding. "; filename = ". basename ($ filename); Header ("Content-Disposition: inline; filename = ". basename ($ filename); readfile ($ filename, "r"); die ();} function useCached ($ filename = "", $ timeout = 3600) {$ this-> _ timeout = $ timeout; if ($ filename = "") {$ filename = $ this-> _ generateFilename ();} if (file_exists ($ filename) & (time ()-filemtime ($ filename) <$ timeout) {$ this-> _ redirect ($ filename );}} function saveFeed ($ filename = "", $ displayContents = true) {if ($ filename = "") {$ filename = $ this-> _ generateFilename ();} $ feedFile = fopen ($ filename, "w +"); if ($ feedFile) {fputs ($ feedFile, $ this-> createFeed (); fclose ($ feedFile ); if ($ displayContents) {$ this-> _ redirect ($ filename) ;}} else {echo"
Error creating feed file, please check write permissions.
";}}} Class FeedDate {var $ unix; function FeedDate ($ dateString =" ") {if ($ dateString = "") $ dateString = date ("r"); if (is_integer ($ dateString) {$ this-> unix = $ dateString; return;} if (preg_match ("~ (? :(?: Mon | Tue | Wed | Thu | Fri | Sat | Sun), \ s + )? (\ D {1, 2}) \ s + ([a-zA-Z] {3}) \ s + (\ d {4 }) \ s + (\ d {2}) :( \ d {2}) :( \ d {2}) \ s + (. *)~ ", $ DateString, $ matches) {$ months = Array (" Jan "=> 1," Feb "=> 2," Mar "=> 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12 ); $ this-> unix = mktime ($ matches [4], $ matches [5], $ matches [6], $ months [$ matches [2], $ matches [1], $ matches [3]); if (substr ($ matches [7], 0, 1) = '+' OR substr ($ matches [7],) = '-') {$ tzOffset = (substr ($ matches [7],) * 60 + substr ($ matches [7],-2 )) * 60;} else {I F (strlen ($ matches [7]) = 1) {$ oneHour = 3600; $ ord = ord ($ matches [7]); if ($ ord <ord ("M") {$ tzOffset = (ord ("A")-$ ord-1) * $ oneHour ;} elseif ($ ord> = ord ("M") & $ matches [7]! = "Z") {$ tzOffset = ($ ord-ord ("M") * $ oneHour;} elseif ($ matches [7] = "Z ") {$ tzOffset = 0 ;}} switch ($ matches [7]) {case "UT": case "GMT": $ tzOffset = 0 ;}} $ this-> unix + = $ tzOffset; return;} if (preg_match ("~ (\ D {4})-(\ d {2})-(\ d {2}) T (\ d {2 }) :( \ d {2}) :( \ d {2 })(. *)~ ", $ DateString, $ matches) {$ this-> unix = mktime ($ matches [4], $ matches [5], $ matches [6], $ matches [2], $ matches [3], $ matches [1]); if (substr ($ matches [7], 0, 1) = '+' OR substr ($ matches [7],) = '-') {$ tzOffset = (substr ($ matches [7) * 60 + substr ($ matches [7],-2) * 60;} else {if ($ matches [7] = "Z ") {$ tzOffset = 0 ;}$ this-> unix + = $ tzOffset; return ;}$ this-> unix = 0;} function rfc822 () {$ date = gmdate ("Y-m-d H: I: s", $ This-> unix); if (TIME_ZONE! = "") $ Date. = "". str_replace (":", "", TIME_ZONE); return $ date;} function iso8601 () {$ date = gmdate ("Y-m-d H: I: s ", $ this-> unix); $ date = substr ($ date, 0, 22 ). ':'. substr ($ date,-2); if (TIME_ZONE! = "") $ Date = str_replace ("+ 00:00", TIME_ZONE, $ date); return $ date;} function unix () {return $ this-> unix ;}} class RSSCreator10 extends FeedCreator {function createFeed () {$ feed ="
Encoding. "\"?> \ N "; $ feed. = $ this-> _ createGeneratorComment (); if ($ this-> cssStyleSheet = "") {$ cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";} $ feed. = $ this-> _ createStylesheetReferences (); $ feed. ="
\ N "; $ feed. ="
SyndicationURL. "\"> \ n "; $ feed. ="
". Htmlspecialchars ($ this-> title )."\ N "; $ feed. ="
". Htmlspecialchars ($ this-> description )."
\ N "; $ feed. ="
". $ This-> link ."\ N "; if ($ this-> image! = Null) {$ feed. ="
Image-> url. "\"/> \ n ";}$ now = new FeedDate (); $ feed. ="
". Htmlspecialchars ($ now-> iso8601 ())."
\ N "; $ feed. ="
\ N "; $ feed. ="
\ N "; for ($ I = 0; $ I
Items); $ I ++) {$ feed. ="
Items [$ I]-> link). "\"/> \ n ";}$ feed. ="
\ N "; $ feed. ="
\ N "; $ feed. ="
\ N "; if ($ this-> image! = Null) {$ feed. ="
Image-> url. "\"> \ n "; $ feed. ="
". $ This-> image-> title ."\ N "; $ feed. ="
". $ This-> image-> link ."\ N "; $ feed. ="
". $ This-> image-> url ."
\ N "; $ feed. ="\ N ";}$ feed. = $ this-> _ createAdditionalElements ($ this-> additionalElements," "); for ($ I = 0; $ I
Items); $ I ++) {$ feed. ="
Items [$ I]-> link). "\"> \ n "; // $ feed. ="
Posting
\ N "; $ feed. ="
Text/html
\ N "; if ($ this-> items [$ I]-> date! = Null) {$ itemDate = new FeedDate ($ this-> items [$ I]-> date); $ feed. ="
". Htmlspecialchars ($ itemDate-> iso8601 ())."
\ N ";} if ($ this-> items [$ I]-> source! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> source )."
\ N ";} if ($ this-> items [$ I]-> author! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> author )."
\ N ";}$ feed. ="
". Htmlspecialchars (strip_tags (strtr ($ this-> items [$ I]-> title," \ n \ r ","")))."\ N "; $ feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> link )."\ N "; $ feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> description )."
\ N "; $ feed. = $ this-> _ createAdditionalElements ($ this-> items [$ I]-> additionalElements," "); $ feed. ="
\ N ";}$ feed. ="\ N "; return $ feed ;}} class RSSCreator091 extends FeedCreator {var $ RSSVersion; function RSSCreator091 () {$ this-> _ setRSSVersion (" 0.91 "); $ this-> contentType = "application/rss + xml";} function _ setRSSVersion ($ version) {$ this-> RSSVersion = $ version;} function createFeed () {$ feed ="
Encoding. "\"?> \ N "; $ feed. = $ this-> _ createGeneratorComment (); $ feed. = $ this-> _ createStylesheetReferences (); $ feed. ="
RSSVersion. "\"> \ n "; $ feed. ="
\ N "; $ feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> title), 100 )."\ N "; $ this-> descriptionTruncSize = 500; $ feed. ="
". $ This-> getDescription ()."
\ N "; $ feed. ="
". $ This-> link ."\ N "; $ now = new FeedDate (); $ feed. ="
". Htmlspecialchars ($ now-> rfc822 ())."
\ N "; $ feed. ="
". FEEDCREATOR_VERSION ."
\ N "; if ($ this-> image! = Null) {$ feed. ="
\ N "; $ feed. ="
". $ This-> image-> url ."
\ N "; $ feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> image-> title), 100 )."\ N "; $ feed. ="
". $ This-> image-> link ."\ N "; if ($ this-> image-> width! = "") {$ Feed. ="
". $ This-> image-> width ."
\ N ";} if ($ this-> image-> height! = "") {$ Feed. ="
". $ This-> image-> height ."
\ N ";} if ($ this-> image-> description! = "") {$ Feed. ="
". $ This-> image-> getDescription ()."
\ N ";}$ feed. ="\ N ";} if ($ this-> language! = "") {$ Feed. ="
". $ This-> language ."
\ N ";} if ($ this-> copyright! = "") {$ Feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> copyright), 100 )."
\ N ";} if ($ this-> editor! = "") {$ Feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> editor), 100 )."
\ N ";} if ($ this-> webmaster! = "") {$ Feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> webmaster), 100 )."
\ N ";} if ($ this-> pubDate! = "") {$ PubDate = new FeedDate ($ this-> pubDate); $ feed. ="
". Htmlspecialchars ($ pubDate-> rfc822 ())."
\ N ";} if ($ this-> category! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> category )."
\ N ";}if ($ this-> docs! = "") {$ Feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> docs), 500 )."
\ N ";} if ($ this-> ttl! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> ttl )."
\ N ";}if ($ this-> rating! = "") {$ Feed. ="
". FeedCreator: iTrunc (htmlspecialchars ($ this-> rating), 500 )."
\ N ";}if ($ this-> skipHours! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> skipHours )."
\ N ";}if ($ this-> skipDays! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> skipDays )."
\ N ";}$ feed. = $ this-> _ createAdditionalElements ($ this-> additionalElements," "); for ($ I = 0; $ I
Items); $ I ++) {$ feed. ="
\ N "; $ feed. ="
". FeedCreator: iTrunc (htmlspecialchars (strip_tags ($ this-> items [$ I]-> title), 100 )."\ N "; $ feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> link )."\ N "; $ feed. ="
". $ This-> items [$ I]-> getDescription ()."
\ N "; if ($ this-> items [$ I]-> author! = "") {$ Feed. = "". htmlspecialchars ($ this-> items [$ I]-> author )."\ N ";}/* // on hold if ($ this-> items [$ I]-> source! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> source )."\ N ";}*/if ($ this-> items [$ I]-> category! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> category )."
\ N ";} if ($ this-> items [$ I]-> comments! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> comments )."
\ N ";} if ($ this-> items [$ I]-> date! = "") {$ ItemDate = new FeedDate ($ this-> items [$ I]-> date); $ feed. ="
". Htmlspecialchars ($ itemDate-> rfc822 ())."
\ N ";} if ($ this-> items [$ I]-> guid! = "") {$ Feed. ="
". Htmlspecialchars ($ this-> items [$ I]-> guid )."
\ N ";}$ feed. = $ this-> _ createAdditionalElements ($ this-> items [$ I]-> additionalElements," "); $ feed. ="
\ N ";}$ feed. ="
\ N "; $ feed. ="
\ N "; return $ feed;} class RSSCreator20 extends RSSCreator091 {function RSSCreator20 () {parent: _ setRSSVersion (" 2.0 ");}}
Example:
Title = "page title"; $ rss-> link = "url http: //"; $ rss-> description = "rss title "; while ($ rowbrs = mysql_fetch_array ($ brs) {$ item = new FeedItem (); $ item-> title = $ rowbrs ['subobject']; $ item-> link = 'HTTP: // www.111cn.net/'; $ item-> description = $ rowbrs ['description']; $ rss-> addItem ($ item );} mysql_close ($ db); $ rss-> saveFeed ("RSS2.0", "rss. xml ");