PHP implementation of the RSS generation class Instance _php tutorial

Source: Internet
Author: User

PHP implementation of the RSS generation class instance


 PHP implementation of the RSS generation class instance

This article mainly introduces the PHP implementation of the RSS generation class, the example analysis of the RSS generation of the principle, definition and use of skills, very practical value, the need for friends can refer to the next

This article is an example of a PHP implementation of the RSS generation class. Share to everyone for your reference. Specific as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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. = " \ n";

$out. = "" . $this->title. "\ n ";

$out. = " ". $this->link. "\ n";

$out. = " ". $this->description. " \ n";

$out. = " ". $this->language. " \ n";

$out. = " ". $this->getpubdate (). " \ n";

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

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

$out. = "\ 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 = ' . "\ n";

$out. = ' . "\ n";

return $out;

}

function Footer ()

{

Return ';

}

}

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. = " \ n";

$out. = "" . $this->title. "\ n ";

$out. = " ". $this->link. "\ n";

$out. = " ". $this->description. " \ n";

$out. = " ". $this->getpubdate (). " \ n";

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

$out. = " ";

if (Empty ($this->guid)) $this->guid = $this->link;

$out. = " ". $this->guid. " \ n";

foreach ($this->tags as $key = + $val) $out. = "< $key > $val ";

$out. = "\ n";

return $out;

}

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

{

$this->attachment = $url;

$this->mimetype = $mimetype;

$this->length = $length;

}

}

Examples of use are:

?

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 = " $html ";

$feed->additem ($item);

}

echo $feed->serve ();

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/988943.html www.bkjia.com true http://www.bkjia.com/PHPjc/988943.html techarticle PHP Implementation of the RSS generation class instance PHP implementation of the RSS Generation Class example This article mainly introduces the PHP implementation of the RSS generation class, the example analysis of the RSS generation of the principle, definition and use of skills, very ...

  • 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.