How to generate RSS subscriptions in php _ PHP Tutorial

Source: Internet
Author: User
Tags rfc822
Php generates an RSS subscription. This article describes how to generate an RSS subscription by using php. Share it with you for your reference. The specific analysis is as follows: RSS (simple information aggregation, also called aggregate content) the method for generating RSS subscriptions by php

This example describes how to generate an RSS subscription using php. Share it with you for your reference. The specific analysis is as follows:

RSS (simple information aggregation, also called aggregate content) is a format for describing and synchronizing website content. RSS can be one of the following three interpretations: Really Simple Syndication; RDF (Resource Description Framework) Site Summary; Rich Site Summary. However, these three interpretations refer to the same Syndication technology. RSS is widely used in online news channels, blogs, and wiki. Using RSS subscriptions can get information faster. The website provides RSS output, which helps users get the latest updates to website content. Network users can read the website content that supports RSS output without opening the website content page by means of RSS-supported aggregation tools on the client.
Technically, an RSS file is a standard XML data. the file generally uses rss, xml, or rdf as the suffix. The following is an example of the content of an rss file:

The code is as follows:




Foot home
Http://www.jb51.net/</link>
Foot home

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:

The code is as follows:

/**
** Php dynamically generates an RSS class
**/
Define ("TIME_ZONE ","");
Define ("FEEDCREATOR_VERSION", "www.jb51.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 ();
}
}

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-> _ feed = 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 $ export stylesheet = "";
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-> your 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],) = '+' OR substr ($ matches [7],) = '-'){
$ TzOffset = (substr ($ matches [7],) * 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")-$ 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],) = '+' 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. =" $ Feed. = "xmlns = \" http://purl.org/rss/1.0/\ "\ n ";
$ Feed. = "xmlns: rdf = \" http://www.w3.org/5o/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. =" 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:

The code is as follows:

Header ('content-Type: text/html; charset = utf-8 ');
$ Db = mysql_connect ('127. 0.0.1 ', 'root', '123 ');
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 = "URL http ://";
$ Rss-> description = "rss title ";
While ($ rowbrs = mysql_fetch_array ($ brs )){
$ Item = new FeedItem ();
$ Item-> title = $ rowbrs ['subobject'];
$ Item-> link = 'http: // www.jb51.net /';
$ Item-> description = $ rowbrs ['description'];
$ Rss-> addItem ($ item );
}
Mysql_close ($ db );
$ Rss-> saveFeed ("RSS2.0", "rss. xml ");

I hope this article will help you with php programming.

Examples in this article describes how to generate an RSS subscription using php. Share it with you for your reference. The specific analysis is as follows: RSS (simple information aggregation, also called aggregate content )...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.