Profil | Mitglieder | Registrieren | Start | Suche


PHP-Support.de » Programmierung » PHP & MySQL » Codeschnipsel » RSS 2.0 Klasse    » Hallo Gast [Login | Registrieren]

Neues Thema | Antworten   

Autor Beitrag
MARK.US
Mitglied
Gruenling


Dabei seit: 19.04.2010
Herkunft: keine Angabe
Posts: 28
     RSS 2.0 Klasse Zitat | Bearbeiten

Die folgende Klasse hilft beim erstellen von RSS 2.0 Feeds.

Features:
+ Erstellt valide RSS 2.0 Feeds
+ Vollständige Unterstützung der RSS 2.0 Spezifikation
+ Kann XHTML ausgeben
+ Unterstützt Caching

Die Klasse:
 PHP 
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:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
<?php
class rss {

    
/*
     * RSS 2.0 Class
     * Copyright: (c) by Markus P. N. (MARK.US at php-einfach.de)
     * Date (RFC 2822): Fri, 29 Oct 2010 23:15:38 +0200
    */

    /* Variables: */
    
protected $mime;
    protected 
$charset;
    protected 
$title;
    protected 
$link;
    protected 
$description;
    protected 
$language;
    protected 
$copyright;
    protected 
$managingEditor;
    protected 
$webMaster;
    protected 
$pubDate;
    protected 
$lastBuildDate;
    protected 
$category;
    protected 
$generator;
    protected 
$docs;
    protected 
$ttl;
    protected 
$textInput;
    protected 
$domain;
    protected 
$starttime;
    protected 
$itemid=0;
    protected 
$caching=FALSE;
    protected 
$cachefile;
    protected 
$cachetime;
    protected 
$items=array();
    
/* Constructor, sends header an sets required variables: */
    
public function  __construct($title$link$description$charset="UTF-8"$starttime=null) {
        if (
stristr($_SERVER["HTTP_ACCEPT"],"application/rss+xml"))
            
$this->mime="application/rss+xml";
        elseif (
stristr($_SERVER["HTTP_ACCEPT"],"application/xml"))
            
$this->mime="application/xml";
        else
            
$this->mime="text/xml";
        
$this->charset=$charset;
        
header("content-type: ".$this->mime."; charset=".$this->charset);
        
$this->title=$title;
        
$this->link=$link;
        
$this->description=$description;
        if(
$starttime === null and $starttime !== false)
            
$this->starttime=microtime(true);
        elseif(
$starttime !== FALSE)
            
$this->starttime=floatval($starttime);
    }
    
/* Activates ($activated=true) or deactivates ($activated=false) the caching,
     * cached data will be ouputted to the file $cachefile,
     * the file will de refreshed after $cachetime seconds: */
    
public function caching($cachefile$activate=true$cachetime=900) {
        
$this->caching=TRUE;
        
$this->cachefile=$cachefile;
        
$this->cachetime=$cachetime;
    }
    
/* Sets the language of the feed: */
    
public function set_language($language) {
        
$this->language=$language;
    }
    
/* Sets the copyright of the feed: */
    
public function set_copyright($copyright) {
        
$this->copyright=$copyright;
    }
    
/* Sets the managing editor of the feed: */
    
public function set_managingEditor($email$name) {
        
$this->managingEditor=array("email" => $email"name" => $name);
    }
    
/* Sets the webmaster: */
    
public function set_webMaster($email$name) {
        
$this->webMaster=array("email" => $email"name" => $name);
    }
    
/* Sets the publication date of the feed: */
    
public function set_pubDate($timestamp) {
        
$this->pubDate=date("r"$timestamp);
    }
    
/* Sets the last build date of the feed: */
    
public function set_lastBuildDate($timestamp) {
        
$this->lastBuildDate=date("r"$timestamp);
    }
    
/* Sets the category of the feed: */
    
public function set_category($category) {
        
$this->category=(array)explode(","$category);
        ;
    }
    
/* Sets the documentation of the feed version: */
    
public function set_docs($docs) {
        
$this->docs=$docs;
    }
    
/* Sets the time to life of the feed: */
    
public function set_ttl($minutes) {
        
$this->ttl=$minutes;
    }
    
/* Sets the image (logo) of the feed: */
    
public function set_image($url) {
        
$this->image=$url;
    }
    
/* Adds a text input box the feed: */
    
public function set_textInput($title$description$name$link) {
        
$this->textInput=array("title" => $title"description" => $description"name" => $name"link" => $link);
    }
    
/* Adds an item to the feed, returns the item ID (required by the following functions): */
    
public function add_item($title$description$link=NULL$html=FALSE) {
        
$this->itemid++;
        if(
$html !== FALSE)
            
$description=htmlentities($descriptionENT_QUOTES$this->charsetTRUE);
        
$this->items[$this->itemid]=array("title" => $title"description" => $description"name" => $name"link" => $link);
        return 
$this->itemid;
    }
    
/* Sets the author of the feed item: */
    
public function item_set_author($email$name$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        
$this->items[$item]["author"]=array("email" => $email"name" => $name);
    }
    
/* Sets the category of the feed item: */
    
public function item_set_category($category$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        
$this->items[$item]["category"]=(array)explode(","$category);
    }
    
/* Sets the comment site of the feed item: */
    
public function item_set_comments($url$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        
$this->items[$item]["comments"]=$url;
    }
    
/* Adds an attachment to the feed item: */
    
public function item_add_enclosure($url$length$type$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        
$this->items[$item]["enclosure"][]=array("url" => $url"length" => $length"type" => $type);
    }
    
/* Sets an unique ID to the feed item: */
    
public function item_set_guid($guid$isPermaLink="true"$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        if(
strval($isPermaLink) != "true")
            
$isPermaLink "false";
        
$this->items[$item]["guid"]=array("guid" => $guid"isPermaLink"  => strval($isPermaLink));
    }
    
/* Sets the publication date of the feed item: */
    
public function item_set_pubDate($timestamp$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        
$this->items[$item]["pubDate"]=date("r"$timestamp);
    }
    
/* Sets the source of the feed item: */
    
public function item_set_source($url$name$item=NULL) {
        if(
$item !== NULL and intval($item) > 0)
            
$item=intval($item);
        else
            
$item=$this->itemid;
        
$this->items[$item]["source"]=array("url" => $url"name" => $name);
    }
    
/* Outputs all item of this feed */
    
private function item_out() {
        
$out="";
        foreach (
$this->items AS $item) {
            
$out .= "<item>
<title>"
.$item["title"]."</title>
<description>"
.$item["description"]."</description>\r\n";
            if(isset (
$item["link"]))
                
$out .= "<link>".$item["link"]."</link>\r\n";
            if(isset (
$item["author"]))
                
$out .= "<author>".$item["author"]["email"]." (".$item["author"]["name"].")</author>\r\n";
            if(isset (
$item["category"]))
                
$out .= "<category>".implode("</category>\r\n<category>"$item["category"])."</category>\r\n";
            if(isset (
$item["comments"]))
                
$out .= "<comments>".$item["comments"]."</comments>\r\n";
            if(isset (
$item["guid"]))
                
$out .= "<guid isPermaLink=\"".$item["guid"]["isPermaLink"]."\">".$item["guid"]["guid"]."</guid>\r\n";
            if(isset (
$item["pubDate"]))
                
$out .= "<pubDate>".$item["pubDate"]."</pubDate>\r\n";
            if(isset (
$item["source"]))
                
$out .= "<source url=\"".$item["source"]["url"]."\">".$item["source"]["name"]."</source>\r\n";
            if(isset (
$item["enclosure"])) {
                foreach (
$item["enclosure"] AS $enclosure)
                    
$out .= "<enclosure url=\"".$enclosure["url"]."\" length=\"".$enclosure["length"]."\" type=\"".$enclosure["type"]."\" />\r\n";
            }
            
$out .= "</item>\r\n";
        }
        return 
$out;
    }
    
/* outputs the feed: */
    
public function output() {
        if(
$this->caching === TRUE and filemtime($this->cachefile) + $this->cachetime time()) {
            
$handle=fopen($this->cachefile"wb");
            
$out=fread($handlefilesize($this->cachefile));
            
fclose($handle);
            if(
$out !== FALSE) {
                echo 
$out;
                return 
TRUE;
            }
        }
        
$out="<?xml version=\"1.0\" ?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>
<title>"
.$this->title."</title>
<link>"
.$this->link."</link>
<description>"
.$this->description."</description>\r\n";
        if(
strlen($this->language) > 0)
            
$out .= "<language>".$this->language."</language>\r\n";
        if(
strlen($this->copyright) > 0)
            
$out .= "<copyright>".$this->copyright."</copyright>\r\n";
        if(
count($this->managingEditor) > 0)
            
$out .= "<managingEditor>".$this->managingEditor["email"]." (".$this->managingEditor["name"].")</managingEditor>\r\n";
        if(
count($this->webMaster) > 0)
            
$out .= "<webMaster>".$this->webMaster["email"]." (".$this->webMaster["name"].")</webMaster>\r\n";
        if(
strlen($this->pubDate) > 0)
            
$out .= "<pubDate>".$this->pubDate."</pubDate>\r\n";
        if(
strlen($this->lastBuildDate) > 0)
            
$out .= "<lastBuildDate>".$this->lastBuildDate."</lastBuildDate>\r\n";
        if(
count($this->category) > 0)
            
$out .= "<category>".implode("</category>\r\n<category>"$this->category)."</category>\r\n";
        if(
strlen($this->docs) > 0)
            
$out .= "<docs>".$this->docs."</docs>\r\n";
        if(
strlen($this->ttl) > 0)
            
$out .= "<ttl>".$this->ttl."</ttl>\r\n";
        if(
strlen($this->image) > 0)
            
$out .= "<image>
<url>"
.$this->image."</url>
<title>"
.$this->title."</title>
<link>"
.$this->link."</link>
</image>\r\n"
;
        if(
count($this->textInput) > 0)
            
$out .= "<textInput>
<title>"
.$this->textInput["title"]."</title>
<description>"
.$this->textInput["description"]."</description>
<name>"
.$this->textInput["name"]."</name>
<link>"
.$this->textInput["link"]."</link>
</textInput>\r\n"
;
        
$out .= "<atom:link href=\"http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."\" rel=\"self\" type=\"application/rss+xml\" />\r\n";
        
$out .= $this->item_out();
        
$out .= "</channel>
</rss>"
;
        if(
$starttime !== FALSE)
            
$out .= "\r\n<!-- Generated in ".number_format(round((microtime(TRUE)-$this->starttime), 2), 2","".")."ms -->";
        if(
$this->caching === TRUE and filemtime($this->cachefile) + $this->cachetime time()) {
            
$handle=fopen($this->cachefile"wb");
            
fwrite($handle$out);
            
fclose($handle);
        }
        echo 
$out;
    }
}
?>



Benutzung:
Die Funktionen, die mit set_ beginnen sind für Globale einstellungen. Die die mit item_set beginnen, sind für die einstellungen eines Eintrags (Standartmäßig der letzte).
Alle Funktionen sind in einem kurzen Kommentar darüber kurz beschrieben, zudem sind die Namen der Parameter Selbsterklärend.
Sollte es trotzdem Unklarheiten geben könnt ihr genaueres in der Spezifikation nachlesen, die Funktionen und Parameter sind nach den Elementen benannt für die sie zuständig sind.

Beispiele:
- Einfaches Feed mit einem Eintrag:
 PHP 
1:
2:
3:
4:
5:
6:
7:
8:
<?php
// erstelle neuen feed
$rss = new rss("feedname""http://127.0.0.1/""feedbeschreibung");
// Füge neuen Eintrag hinzu
$rss->add_item("titel vom Eintrag""inhalt des eintrags");
// Gebe Feed aus
$rss->output();
?>

- Feed mit Logo und Eintrag mit Anhang:
 PHP 
1:
2:
3:
4:
5:
6:
7:
8:
9:
<?php
$rss 
= new rss("feedname""http://127.0.0.1/""feedbeschreibung");
// füge logo zum feed hinzu
$rss->set_image("http://127.0.0.1/logo.gif");
$rss->add_item("titel vom Eintrag""inhalt des eintrags");
// füge anhang zum Eintrag hinzu
$rss->item_add_enclosure("http://127.0.0.1/anhang.zip""12216320""application/zip");
$rss->output();
?>

- Feed aus dem ersten Beispiel mit Caching:
 PHP 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
<?php
// erstelle neuen feed
$rss = new rss("feedname""http://127.0.0.1/""feedbeschreibung");
// Füge neuen Eintrag hinzu
$rss->add_item("titel vom Eintrag""inhalt des eintrags");

// Aktiviert das Caching, die gecachten Daten sind 60 Sekunden lang gültig
$rss->caching("rss_cache.xml"true60);

// Gebe Feed aus
$rss->output();
?>

In den Obrigen beispielen muss die Klasse natürlich auch eingebunden werden.


Kommentare und Vorschläge zur Verbesserung sind erwünscht.


30.10.2010, 00:01 Profil | PM | E-Mail  
Seiten (1):  1 
PHP-Support.de » Programmierung » PHP & MySQL » Codeschnipsel » RSS 2.0 Klasse   

Neues Thema | Antworten   


Powered by Command Board 1.0 - Beta 2.0 © 2004-08 PHP-Einfach | Impressum | Datenschutz