RSS, also called aggregated content, is a format for describing and synchronizing web site content. The following code to generate an RSS subscription:
RSS XML Structure
<?xml version= "1.0" encoding= "Utf-8"?> <rss version=
"2.0" >
<channel>
<title> Website name </title>
<link>http://www.111cn.net/</link>
<description> website Description!</ description>
<item>
<title>rss tutorial</title>
<link> website address/rss</link >
<description>new RSS tutorial on w3school</description>
</item>
<item>
<title>xml tutorial</title>
<link> website address/xml</link>
<description> New XML tutorial on w3school</description>
</item>
</channel>
</rss>
RSS instances
<?php class rss {public function createfeed () {//rss head $webUrl = ' http://' $_server[' Http_host '];//website address $webName = ' website name '; //website name $webDesc = '
Description of the Web site $html = ' <?xml version= ' 1.0 ' encoding= ' utf-8 ' // <rss version= "2.0" > <channel> <title> '. $webName. ' </title> <link> '. $webUrl. ' </link> <description> '. $webDesc. '
</description> $this->createitem (). ' </channel> </rss> ';
echo $html; Private function createitem () {//rss item//$data can be replaced with their 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 .= ' <item> <title> '. $data [' title ']. ' </title> <link> '. $data [' link ']. ' </link> <pubDate> '. $data [' Date ']. ' </pubDate> <description><! [cdata['. $data [' description '].]
></description> </item> ';
} return $html;
} header ("Content-type: text/xml; charset=utf-8");
$rss = new rss ();
$rss->createfeed (); Exit;?>
After the RSS feed is generated, how to set up to add RSS to the website? and have Firefox, IE7, or other Feed bots automatically discovered? Simply add a specific Link tag to the head section of the page, as follows:
<link rel= "Alternate" type= "Application/rss+xml" title= "website name RSS feed" href= "http://www.111cn.net"/>
Set title as feed title, href as feed address, everything OK!
Example 2
<?xml version= "1.0" encoding= "Utf-8"?> <rss version=
"2.0" >
<channel>
<title> PHP Programmer Tutorial Network </title>
<link>http://www.111cn.net/</link>
<description> This site is a PHP programmer working Life notes!</description>
<item>
<title>rss tutorial</title>
< link> Web address/rss</link>
<description>new RSS tutorial on w3school</description>
</ item>
<item>
<title>xml tutorial</title>
<link> website address/xml</link>
<description>new XML tutorial on w3school</description>
</item>
</channel>
</rss>
Share the following code example for dynamically generating RSS using PHP:
<?php/** ** php Dynamic Generation RSS class **/define ("Time_zone", "");
Define ("Feedcreator_version", "www.111cn.net");//Your website class feeditem extends htmldescribable{
var $title, $description, $link;
var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
var $date;
var $additionalElements =array (); class feedimage extends htmldescribable{ var $title, $url, $link var $width
, $height, $description;
class htmldescribable{ var $descriptionHtmlSyndicated var $descriptionTruncSize; function getdescription () { $descriptionField =new feedhtmlfield ($this->
Description);
$descriptionField->syndicatehtml= $this->descriptionhtmlsyndicated;
$descriptionField->truncsize= $this->descriptiontruncsize;
return $descriptionField->output (); &NBSP}} class feedhtmlfield{&NBsp;var $rawFieldContent;
var $truncSize, $syndicateHtml; function feedhtmlfield ($parFieldContent) { if ($parFieldContent) { $this-
>rawfieldcontent= $parFieldContent; &NBSP;&NBSP}  } function output () { if (! $this->rawfieldcontent) {
$result = ""; } elseif ($this->syndicatehtml) { $result = "<![ cdata[". $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; &NBSP}} class universalfeedcreator extends feedcreator{ var $_feed; function& nbsp; _setformat ($format) { switch (Strtoupper ($format)) { case "2.0": // fall through case "RSS2.0": $this-
>_feed=new rsscreator20 ();
break; case "0.91": // fall through case
"RSS0.91": $this->_feed=new rsscreator091 ();
break;
default: $this->_feed=new rsscreator091 ();
break;
&NBSP;&NBSP} $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 "," Encodi ")Ng ")) { $this->_feed->{$key}= $this->{$key}; } } &NBSP} function createfeed ($format = "RSS0.91") { $this->_setformat ($format);
return $this->_feed->createfeed (); &NBSP} function savefeed ($format = "RSS0.91", $filename = "", $displayContents =true) { $this-
>_setformat ($format);
$this->_feed->savefeed ($filename, $displayContents); &NBSP} function usecached ($format = "RSS0.91", $filename = "", $timeout =3600) { $this->_
SetFormat ($format);
$this->_feed->usecached ($filename, $timeout);
&NBSP} 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 $xslStyleSheet = ""; 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, "."); &NBSP;&NBSP} if ($pos >= $length *0.4) { return substr ($string, 0, $pos + 1). "
... ";
&NBSP;&NBSP} $pos =strrpos ($string, " "); if ($pos >= $length-4) { $string =substr ($string, 0, $length-4); $
Pos=strrpos ($string, " "); &NBSP;&NBSP} if ($pos >= $length *0.4) {&NBSP;&NBSP;&NBsp;return substr ($string, 0, $pos). "
... "; &NBSP;&NBSP} return substr ($string, 0, $length-4). "
... "; &NBSP} function _creategeneratorcomment () { return <!-- generator=\ "". Feedcreator_version. "
\ " -->\n"; &NBSP} function _createadditionalelements ($elements, $indentString = "") { $ae = "";
if (Is_array ($elements)) { foreach ($elements AS $key => $value) { $ae .= $indentString. "
< $key > $value </$key >\n ";
} return $ae; &NBSP} function _createstylesheetreferences () { $xml = ""; if ($this-> Cssstylesheet) $xml .= "<?xml-stylesheet href=\" ". $this->cssstylesheet."
\ " type=\" text/css\ ">\n"; if ($this->xslstylesheet) $xml .= "<?xml-stylesheet href=\". $this-> XslstylEsheet. "
\ " type=\" text/xsl\ ">\n";
return $xml; &NBSP} function createfeed () {} function _generatefilename () { $fileInfo =
PathInfo ($_server["php_self"]); return substr ($fileInfo ["basename"],0,-(strlen ($fileInfo ["extension"]) +1). ".
XML "; &NBSP} 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 ();
&NBSP} function usecached ($filename = "", $timeout =3600) { $this->_timeout= $timeout; if ($filename = = "") { $filename = $this->_generatefilename (); }
if (file_exists ($filename) && (Time ()-filemtime ($filename) < $timeout)) { $this->_redirect ($filename); &NBSP;&NBSP}  } 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); &NBSP} }else{ echo "<br /><b>error creating feed
file, please check write permissions.</b><br /> ";
&NBSP;&NBSP}  } class feeddate{ var $unix; function feeddate ($dateString = "") {
if ($dateString = = "") $dateString =date ("R");
if (Is_integer ($dateString)) { $this->unix= $dateString; return; } &Nbsp; 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 ("The =>1", "Feb" =>2, "Mar" =>3, "APR" =>4, "may" =>5, "June" =>6, "" "=>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],0,1) = = '] { $tzOffset = (substr ($matches [7],0,3) * 60 + substr ($matches [7],-2)] *
60; }else{ if (strlen ($matches [7]) ==1) { $
onehour=3600;
$ord =ord ($matches [7]); if ($ord < ord ("M")) { $tzOffset = ( Ord ("A") -&nbSP; $ord - 1) * $oneHour; } elseif ($ord >= ord ("M") && $matches [7]!=] Z
") { $tzOffset = ($ord - ord (" M ")) * $oneHour;
} elseif ($matches [7]== "Z") { $tzOffset = 0;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}  } switch ($matches [7]) { case "UT": case "GMT":
$tzOffset = 0;
&NBSP;&NBSP;&NBSP;&NBSP}   $this->unix += $tzOffset;
return; &NBSP;&NBSP} 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],0,1] = = ') { $tzOffset = (substr ($
matches[7],0,3) * 60 + substr ($matches [7],-2)) * 60; }else{ if ($matches [7]== "Z") { $tzOffset =0
;
&NBSP;&NBSP;&NBSP;&NBSP}   $this->unix += $tzOffset;
return;
&NBSP;&NBSP} $this->unix=0; &NBSP} function rfc822 () { $date =gmdate ("Y-m-d h:i:s", $this->unix); if
(time_zone!= "") $date .= " " Str_replace (":", "", Time_zone);
return $date; &NBSP} 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 = "<?xml version=\" 1.0\ Encoding=\ "". $this->encoding. "
\ ">\n";
$feed .= $this->_creategeneratorcomment (); if ($this->cssstylesheet== "") { $cssStyleSheet = "http://www.w3.org/2000/08/
W3c-synd/style.css ";
&NBSP;&NBSP} $feed .= $this->_createstylesheetreferences ();
$feed .= "<rdf:rdf\n";
$feed .= " xmlns=\" http://purl.org/rss/1.0/\ "\ n"; $feed .= " xmlns:rdf=\" http://www.w3.org/1999/02/22-rdf-syntax-ns#\ "\ n"
;
$feed .= " xmlns:slash=\" http://purl.org/rss/1.0/modules/slash/\ "\ n";
$feed .= " xmlns:dc=\" http://purl.org/dc/elements/1.1/\ ">\n"; $feed .= " <channel rdf:about=\" ". $this->syndicationurl."
\ ">\n"; $feed .= " <title>" Htmlspecialchars ($ This->title). "
</title>\n "; $feed .= " <description>". Htmlspecialchars ($this->description). "
</description>\n "; $feed .= " <link>" $this->link. "
</link>\n "; if ($this->image!=null) { $feed .= " <image rdf:resource=\ "". $this->image->url. "
\ " />\n";
&NBSP;&NBSP} $now =new feeddate (); $feed .= " <dc:date>" Htmlspecialchars ($now- >iso8601 ()). "
</dc:date>\n "; $feed .= " &Nbsp;<items>\n "; $feed .= " <rdf:seq
>\n "; for ($i =0 $i <count ($this->items); $i + +) { $feed .= " &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;<RDF:LI&NBSP;RDF: Resource=\ "". Htmlspecialchars ($this->items[$i]->link). "
\ "/>\n"; &NBSP;&NBSP} $feed .= "
</rdf:seq>\n ";
$feed .= " </items>\n";
$feed .= " </channel>\n"; if ($this->image!=null) { $feed .= " <image Rdf:about=\ "". $this->image->url. "
\ ">\n"; $feed .= " <title>". $this-> Image->titLe. "
</title>\n "; $feed .= " <link>". $this->image- >link. "
</link>\n "; $feed .= " <url>". $this->image- >url. "
</url>\n ";
$feed .= " </image>\n"; &NBSP;&NBSP} $feed .= $this->_createadditionalelements ($this->additionalelements, "
"); for ($i =0 $i <count ($this->items); $i + +) { $feed .= " <item rdf:about=\ "". Htmlspecialchars ($this->items[$i]->link). "
\ ">\n"; //$feed .= " <dc:type>posting</
Dc:type>\n "; $feed .= " <dc:format>text/html<
/dc:format>\n "; if ($this->items[$i]->date!=null) { $itemDate =new feeddate ($this->items[$i]->date); $feed .= " <dc:date>". Htmlspecialchars ($itemDate->iso8601 ()). "
</dc:date>\n "; &NBSP;&NBSP;&NBSP} if ($this->items[$i]->source!= "") { $feed. = " <dc:source>" Htmlspecialchars ($this->items[$i] ->source). "
</dc:source>\n "; &NBSP;&NBSP;&NBSP} if ($this->items[$i]->author!= "") { $feed. = " <dc:creator>" Htmlspecialchars ($this->items[$i ]->author). "
</dc:creator>\n "; &NBSP;&NBSP;&NBSP} $feed .= " <title > ". Htmlspecialchars (Strip_tags (STRTR ($this->items[$i]->title," \n\r "," "))." </titlE>\n "; $feed .= " <link>". Htmlspecialchars ($this->items[$i]->link). "
</link>\n "; $feed .= " <description>". Htmlspecialchars ($this->items[$i]->description). "
</description>\n "; $feed .= $this->_createadditionalelements ($this->items[$i]->additionalelements,
" ");
$feed .= " </item>\n";
&NBSP;&NBSP} $feed .= "</rdf:rdf>\n";
return $feed; &NBSP} class rsscreator091 extends feedcreator{ var $RSSVersion function RSSCreator091 () { $this->_setrssversion ("0.91") $this->contenttype= "application/
Rss+xml "; &NBSP} function _setrssversion ($version) { $this->rssversion= $version; &NBSP} function createfeed () { $feed = "<?xml version=\" 1.0\ " encoding=\" ". $ This->encoding. "
\ ">\n";
$feed .= $this->_creategeneratorcomment ();
$feed .= $this->_createstylesheetreferences (); $feed .= "<rss version=\" ". $this->rssversion."
\ ">\n";
$feed .= " <channel>\n"; $feed .= " <title>". Feedcreator::itrunc (Htmlspecialchars ($this->title), 100). "
</title>\n ";
$this->descriptiontruncsize=500; $feed .= " <description>". $this-> GetDescription (). "
</description>\n "; $feed .= " <link>" $this->link. "
</link>\n ";
$now =new feeddate (); $feed .= " &nbsP; <lastbuilddate> ". Htmlspecialchars ($now->rfc822 ())."
</lastbuilddate>\n "; $feed .= " <generator>". Feedcreator_version. "
</generator>\n "; if ($this->image!=null) { $feed .= "
<image>\n "; $feed .= " < Url> ". $this->image->url."
</url>\n "; $feed .= " < Title> ". Feedcreator::itrunc (Htmlspecialchars ($this->image->title), 100). "
</title>\n "; $feed .= " < Link> ". $this->image->link."
</link>\n "; if ($this->image->width!= "") { $feed.= " <width>". $this-> Image->width. "
</width>\n "; &NBSP;&NBSP;&NBSP} if ($this->image->height!= "") { $feed .= <height> ". $this->image-> Height. "
</height>\n "; &NBSP;&NBSP;&NBSP} if ($this->image->description!= "") { $feed. = " <description>" $this-> Image->getdescription (). "
</description>\n "; &NBSP;&NBSP;&NBSP} $feed .= " </image
>\n "; &NBSP;&NBSP} if ($this->language!= "") { $feed .= " <language> ". $this->language."
</language>\n "; } if ($this->copyright!= "") { $feed .= " <copyright> ". Feedcreator::itrunc (Htmlspecialchars ($this->copyright), 100). "
</copyright>\n "; &NBSP;&NBSP} if ($this->editor!= "") { $feed .= " <managingEditor> ". Feedcreator::itrunc (Htmlspecialchars ($this->editor), 100). "
</managingeditor>\n "; &NBSP;&NBSP} if ($this->webmaster!= "") { $feed .= " <webMaster> ". Feedcreator::itrunc (Htmlspecialchars ($this->webmaster), 100). "
</webmaster>\n "; &NBSP;&NBSP} if ($this->pubdate!= "") { $pubDate =new feeddate ($this->
pubdate); $feed .= " <pubDate>". Htmlspecialchars ($pubDate->rfc822 ()). "
</pubdate>\n "; } &nBsp; if ($this->category!= "") { $feed .= " <category> ". Htmlspecialchars ($this->category)."
</category>\n "; &NBSP;&NBSP} if ($this->docs!= "") { $feed .= " <docs> ". Feedcreator::itrunc (Htmlspecialchars ($this->docs), 500). "
</docs>\n "; &NBSP;&NBSP} if ($this->ttl!= "") { $feed .= " <ttl> ". Htmlspecialchars ($this->ttl)."
</ttl>\n "; &NBSP;&NBSP} if ($this->rating!= "") { $feed .= " <rating> ". Feedcreator::itrunc (Htmlspecialchars ($this->rating), 500). "
</rating>\n "; &NBSP;&NBSP} if ($this->skiphours!= "") { $feed .= " <skipHours> ". HtmlspecIalchars ($this->skiphours). "
</skiphours>\n "; &NBSP;&NBSP} if ($this->skipdays!= "") { $feed .= " <skipDays> ". Htmlspecialchars ($this->skipdays)."
</skipdays>\n "; &NBSP;&NBSP} $feed .= $this->_createadditionalelements ($this->additionalelements, "
"); for ($i =0 $i <count ($this->items); $i + +) { $feed .= "
<item>\n "; $feed .= " < Title> ". Feedcreator::itrunc (Htmlspecialchars (strip_tags ($this->items[$i]->title)), 100. "
</title>\n "; $feed .= " < Link> ". Htmlspecialchars ($this->items[$i]->link)."
</link>\n "; $feed .= "&Nbsp; <description> ". $this->items[$i ]->getdescription (). "
</description>\n "; if ($this->items[$i]->author!= "") { $feed .= " <author> ". Htmlspecialchars ($this->items[$ I]->author). "
</author>\n "; &NBSP;&NBSP;&NBSP} /* // on hold if ( $this->items[$i]->source!= "") { $feed .= " <source> ". Htmlspecialchars ($this->items[$i]->source)."
</source>\n ";
&NBSP;&NBSP;&NBSP;&NBSP} */ if ($this->items[$i]->category!= "") { $feed .= " <category> ".Htmlspecialchars ($this->items[$i]->category). "
</category>\n "; &NBSP;&NBSP;&NBSP} if ($this->items[$i]->comments!= "") { $feed. = " <comments>". Htmlspecialchars ($this->items[$i]->comments). "
</comments>\n "; &NBSP;&NBSP;&NBSP} if ($this->items[$i]->date!= "") { $itemDate =
New feeddate ($this->items[$i]->date); $feed .= " <pubDate> ". Htmlspecialchars ($itemDate->rfc822 ())."
</pubdate>\n "; &NBSP;&NBSP;&NBSP} if ($this->items[$i]->guid!= "") { $feed. = " <guid>". Htmlspecialchars ( $this->items[$i]->guid). "
</guid>\n "; } $feed .= $this->_createadditionalelements ($this->items[$i]->additionalelements, "
");
$feed .= " </item>\n";
&NBSP;&NBSP} $feed .= " </channel>\n";
$feed .= "</rss>\n";
return $feed; &NBSP}} class rsscreator20 extends rsscreator091{ function rsscreator20 () {
parent::_setrssversion ("2.0"); &NBSP}}
Use examples:
<?php
header (' content-type:text/html; Charset=utf-8 ');
$db =mysql_connect (' 127.0.0.1 ', ' root ', ' 123456 ');
mysql_query ("Set names UTF8");
mysql_select_db (' dbname ', $db);
$brs =mysql_query (' SELECT * from article ORDER by add_time desc limit 0,10 ', $db);
$rss =new universalfeedcreator ();
$rss->title= "page title";
$rss->link= "website http://";
$rss->description= "RSS title";
while ($rowbrs =mysql_fetch_array ($brs)) {
$item =new feeditem ();
$item->title = $rowbrs [' subject '];
$item->link= ' http://www.111cn.net/';
$item->description = $rowbrs [' description '];
$rss->additem ($item);
}
Mysql_close ($db);
$rss->savefeed ("RSS2.0", "Rss.xml");