RSS generation instance implemented by php

Source: Internet
Author: User

RSS generation instance implemented by php

RSS generation instance implemented by php

This article mainly introduces the RSS generation class implemented by php. The example analyzes the principles, definitions, and usage skills of the RSS generation class, which is very useful. For more information, see

This example describes the RSS generation class implemented by php. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

Class RSS

{

Var $ title;

Var $ link;

Var $ description;

Var $ language = "en-us ";

Var $ pubDate;

Var $ items;

Var $ tags;

Function RSS ()

{

$ This-> items = array ();

$ This-> tags = array ();

}

Function addItem ($ item)

{

$ This-> items [] = $ item;

}

Function setPubDate ($ when)

{

If (strtotime ($ when) = false)

$ This-> pubDate = date ("D, d m y h: I: s", $ when). "GMT ";

Else

$ This-> pubDate = date ("D, d m y h: I: s", strtotime ($ when). "GMT ";

}

Function getPubDate ()

{

If (empty ($ this-> pubDate ))

Return date ("D, d m y h: I: s"). "GMT ";

Else

Return $ this-> pubDate;

}

Function addTag ($ tag, $ value)

{

$ This-> tags [$ tag] = $ value;

}

Function out ()

{

$ Out = $ this-> header ();

$ Out. = "<channel> \ n ";

$ Out. = "<title>". $ this-> title. "</title> \ n ";

$ Out. = "<link>". $ this-> link. "</link> \ n ";

$ Out. = "<description>". $ this-> description. "</description> \ n ";

$ Out. = "<language>". $ this-> language. "</language> \ n ";

$ Out. = "<pubDate>". $ this-> getPubDate (). "</pubDate> \ n ";

Foreach ($ this-> tags as $ key => $ val) $ out. = "<$ key> $ val </$ key> \ n ";

Foreach ($ this-> items as $ item) $ out. = $ item-> out ();

$ Out. = "</channel> \ n ";

$ Out. = $ this-> footer ();

$ Out = str_replace ("&", "&", $ out );

Return $ out;

}

Function serve ($ contentType = "application/xml ")

{

$ Xml = $ this-> out ();

Header ("Content-type: $ contentType ");

Echo $ xml;

}

Function header ()

{

$ Out = '<? Xml version = "1.0" encoding = "UTF-8"?> '. "\ N ";

$ Out. = '<rss version = "2.0" xmlns: dc = "http://purl.org/dc/elements/1.1/">'. "\ n ";

Return $ out;

}

Function footer ()

{

Return '</rss> ';

}

}

Class RSSItem

{

Var $ title;

Var $ link;

Var $ description;

Var $ pubDate;

Var $ guid;

Var $ tags;

Var $ attachment;

Var $ length;

Var $ mimetype;

Function RSSItem ()

{

$ This-> tags = array ();

}

Function setPubDate ($ when)

{

If (strtotime ($ when) = false)

$ This-> pubDate = date ("D, d m y h: I: s", $ when). "GMT ";

Else

$ This-> pubDate = date ("D, d m y h: I: s", strtotime ($ when). "GMT ";

}

Function getPubDate ()

{

If (empty ($ this-> pubDate ))

Return date ("D, d m y h: I: s"). "GMT ";

Else

Return $ this-> pubDate;

}

Function addTag ($ tag, $ value)

{

$ This-> tags [$ tag] = $ value;

}

Function out ()

{

$ Out. = "<item> \ n ";

$ Out. = "<title>". $ this-> title. "</title> \ n ";

$ Out. = "<link>". $ this-> link. "</link> \ n ";

$ Out. = "<description>". $ this-> description. "</description> \ n ";

$ Out. = "<pubDate>". $ this-> getPubDate (). "</pubDate> \ n ";

If ($ this-> attachment! = "")

$ Out. = "<enclosure url = '{$ this-> attachment}' length = '{$ this-> length}' type = '{$ this-> mimetype}'/>";

If (empty ($ this-> guid) $ this-> guid = $ this-> link;

$ Out. = "<guid>". $ this-> guid. "</guid> \ n ";

 

Foreach ($ this-> tags as $ key => $ val) $ out. = "<$ key> $ val </$ key \ n> ";

$ Out. = "</item> \ n ";

Return $ out;

}

Function enclosure ($ url, $ mimetype, $ length)

{

$ This-> attachment = $ url;

$ This-> mimetype = $ mimetype;

$ This-> length = $ length;

}

}

Example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$ Feed = new RSS ();

$ Feed-> title = "RSS Feed Title ";

$ Feed-> link = "http://website.com ";

$ Feed-> description = "Recent articles on your website .";

$ Db-> query ($ query );

$ Result = $ db-> result;

While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC ))

{

$ Item = new RSSItem ();

$ Item-> title = $ title;

$ Item-> link = $ link;

$ Item-> setPubDate ($ create_date );

$ Item-> description = "<! [CDATA [$ html]> ";

$ Feed-> addItem ($ item );

}

Echo $ feed-> serve ();

I hope this article will help you with php programming.

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.