RSS Feeds selber erstellen - Die Klasse
Erstellt am 23.12.2008 um 21:28 Uhr von sim4000
Zu guter letzt noch die Klasse die den XML Code generiert.
Inhalte dieses Artikels
Die Klasse
| 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 | /** * Klasse zur Ausgabe des Contents aus der Datenbank * @author Christian Blechert * @since Juni 2008 * */ class rss_writer { private $chan_title; // Channel Titel private $chan_desc; // Channel Beschreibung private $chan_lang; // Sprache private $chan_link; // URL zu den Eintraegen private $rss_file_url; // URL zum RSS Feed private $entrys; // Eintraege als Array /** * Construct * * @param String $channel_title Channel Titel * @param String $channel_desc Beschreibung * @param String $channel_link URL zu den Eintraegen * @param String $channel_language Sprache * @param String $rss_file_url URL zum RSS Feed */ function __construct($channel_title, $channel_desc, $channel_link, $channel_language, $rss_file_url) { $this->chan_title = $channel_title; $this->chan_desc = $channel_desc; $this->chan_lang = $channel_language; $this->chan_link = $channel_link; $this->rss_file_url = $rss_file_url; $this->entrys = array(); } /** * Bereitet einen String fuer das XML auf * @param String $string * @return String */ function cleanElement($string) { $string = trim($string); $string = str_replace(array("\\n","\\r","\\r\\n"),"", $string); $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); return $string; } /** * Prueft einen URL auf richtige Syntax * * @param String $url Website Adresse * @return String URL * @return false */ function checkUrl($url) { if(substr($url, 0, 7)!="http://" && substr($url, 0, 8)!="https://") { $url = "http://".$url; } $regex = "!^((ftp|(http(s)?))://)?(\\.?([a-z0-9-]+))+\\.[a-z]{2,6}(:[0-9]{1,5})?(/[a-zA-Z0-9.,;\\?|\\'+&%\\$#=~_-]+)*$!i"; if(preg_match($regex, $url)) { return $url; } else { return false; } } /** * Einen Eintrag hinzufuegen * * @param String $title Titel * @param String $desc Text * @param String $pub_date Erstelldatum * @param String $url Link * @param String $author Autor * @param String $author_email Email Adresse Autor * @param String $guid Einzigartiger Schluessel * @return unknown_type */ function add_entry($title, $desc, $pub_date, $url, $author, $author_email, $guid) { $datetime = explode(" ", $pub_date); $date = explode("-", $datetime[0]); $time = explode(":", $datetime[1]); $timestamp = mktime ($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]); $this->entrys[] = array( "title" => $title, "description" => $desc, "date" => date("D, d M Y H:i:s O", $timestamp), "url" => $url, "author" => $author, "author_email" => $author_email, "guid" => $guid ); } /** * Gibt den XML Code aus * @return String */ function get_source() { $rss_source = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n\\n" . "<rss version=\\"2.0\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">\\n\\n"; $rss_source .= " <channel>\\n"; $rss_source .= " <title>".$this->cleanElement($this->chan_title)."</title>\\n"; $rss_source .= " <link>".$this->chan_link."</link>\\n"; $rss_source .= " <description>".$this->cleanElement($this->chan_desc)."</description>\\n"; $rss_source .= " <language>".$this->chan_lang."</language>\\n"; $rss_source .= " <atom:link href=\\"".$this->rss_file_url."\\" rel=\\"self\\" type=\\"application/rss+xml\\" />\\n\\n"; for($i=0; $i<count($this->entrys); $i++) { $rss_source .= " <item>\\n"; $rss_source .= " <title>".$this->cleanElement($this->entrys[$i]['title'])."</title>\\n"; $rss_source .= " <description>".$this->cleanElement($this->entrys[$i]['description'])."</description>\\n"; $rss_source .= " <pubDate>".$this->entrys[$i]['date']."</pubDate>\\n"; $rss_source .= " <link>".$this->entrys[$i]['url']."</link>\\n"; if(strlen($this->entrys[$i]['author_email'])>0 && strlen($this->entrys[$i]['author'])>0) { $rss_source .= " <author>".$this->entrys[$i]['author_email']." (".$this->cleanElement($this->entrys[$i]['author']).")</author>\\n"; } if($this->checkUrl($this->entrys[$i]['guid'])) { $rss_source .= " <guid>".$this->entrys[$i]['guid']."</guid>\\n"; } else { $rss_source .= " <guid isPermaLink='false'>".$this->entrys[$i]['guid']."</guid>\\n"; } $rss_source .= " </item>\\n\\n"; } $rss_source .= " </channel>\\n"; $rss_source .= "</rss>"; return $rss_source; } } |
Hinweise
Falls bei einem Eintrag der Autor oder die Email Adresse des Autors leer bzw ungültig ist, wird das Autor Tag ausgeblendet. Es ist beides zwingend notwendig, um den Autor Anzeigen zu lassen.
Bei dem guid Tag wird geprüft, ob es sich um eine URL handelt. Falls dies nicht der Fall ist, wird das Attribut isPermaLink auf false gesetzt.




