









Voici un classe qui permet de générer facilement un flux RSS 2.0.
La classe peu écrire le flux dans un fichier ou le renvoyer sous forme de chaîne.
<?php class MakeRSS { function MakeRSS($encoding='') { $this->Me = 'MakeRSS0.1'; $this->Ref['encoding'] ='ISO-8859-15'; $this->Ref['protocole'] ='http://'; $this->Ref['langue'] ='fr'; $this->Ref['pubdate'] =date("D, j M Y H:i:s O"); $this->Ref['itemdate'] =date('Y-m-d').'T'.date('H:i:s').'Z'; $this->Ref['format'] ='text/html'; $this->Encoding=($encoding=='')?$this->Ref['encoding']:$encoding; $this->RSS = '<?xml version="1.0" encoding="'.$this->Encoding.'"?>'."\n"; $this->RSS .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">'."\n"; $this->RSS .= '<channel>'."\n"; } function _E($v) { return htmlspecialchars($v, ENT_QUOTES,$this->Encoding); } function ShowRSS($file='') { $this->RSS .= '</channel>'."\n"; $this->RSS .= '</rss>'; $this->RSS = rtrim($this->RSS); if ($file!='') { if (!$handle = fopen($file, 'w')) return false; //Impossible d'ouvrir le fichier if (fwrite($handle, $this->RSS) === FALSE) return false; //Impossible d'écrire dans le fichier fclose($handle); return true; } return $this->RSS; } function Header($titre='',$link='',$description='',$lang='',$auteur='',$copyright='',$pubdate='') { $this->RSS .= '<title>'.$this->_E($titre).'</title>'."\n"; $this->RSS .= '<link>'.((strpos($link,'://')===false)?$this->Ref['protocole'].$link:$link).'</link>'."\n"; $this->RSS .= '<description>'.$this->_E($description).'</description>'."\n"; $this->RSS .= '<language>'.(($lang=='')?$this->Ref['langue']:$lang).'</language>'."\n"; $this->RSS .= '<dc:creator>'.$this->_E($auteur).'</dc:creator>'."\n"; $this->RSS .= '<copyright>'.$this->_E($copyright).'</copyright>'."\n"; $this->RSS .= '<pubDate>'.(($pubdate=='')?$this->Ref['pubdate']:$pubdate).'</pubDate>'."\n"; $this->RSS .= '<generator>'.$this->Me.'</generator>'."\n"; } function Image($titre='',$url='',$link='',$description='') { $this->RSS .= '<image>'."\n"; $this->RSS .= '<title>'.$this->_E($titre).'</title>'."\n"; $this->RSS .= '<url>'.((strpos($url,'://')===false)?$this->Ref['protocole'].$url:$url).'</url>'."\n"; if (($link=='') && ($url!='')) { $link=explode('/', ((strpos($url,'://')===false)?$this->Ref['protocole'].$url:$url), 4); array_pop($link); $link=implode($link,'/'); } $this->RSS .= '<link>'.$link.'</link>'."\n"; $this->RSS .= '<description>'.$this->_E($description).'</description>'."\n"; $this->RSS .= '</image>'."\n"; } function AddItem($titre='',$link='',$date='',$format='',$lang='',$auteur='',$description='',$contenu='',$categorieLink='',$categorieName='') { $this->RSS .= '<item>'."\n"; $this->RSS .= '<title>'.$this->_E($titre).'</title>'."\n"; $this->RSS .= '<link>'.((strpos($link,'://')===false)?$this->Ref['protocole'].$link:$link).'</link>'."\n"; $this->RSS .= '<dc:date>'.(($date=='')?$this->Ref['itemdate']:$date).'</dc:date>'."\n"; $this->RSS .= '<dc:format>'.(($format=='')?$this->Ref['format']:$format).'</dc:format>'."\n"; $this->RSS .= '<dc:language>'.(($lang=='')?$this->Ref['langue']:$lang).'</dc:language>'."\n"; $this->RSS .= '<dc:creator>'.$this->_E($auteur).'</dc:creator>'."\n"; $this->RSS .= '<description>'.$this->_E($description).'</description>'."\n"; $this->RSS .= '<content:encoded>'.$this->_E($contenu).'</content:encoded>'."\n"; $this->RSS .= '<category domain="'.(((strpos($categorieLink,'://')===false) && ($categorieLink!=''))?$this->Ref['protocole'].$categorieLink:$categorieLink).'">'.$this->_E($categorieName).'</category>'."\n"; $this->RSS .= '</item>'."\n"; } } ?>
Voici un exemple d'utilisation commenté :
//instancie la classe $a=new MakeRSS(); //Ajoute la description du flux $a->Header('mon flux','www.chezmoi.com','cool non ?','','iDo','yapas',''); //Ajoute (au besoin) une image qui va avec la description du flux $a->Image('mon logo','www.plop.com/hop/hip/j.jpg','','bo logo non ?'); //Ajoute un article au flux (repetez cette ligne pour chaque article) $a->AddItem('teste&<','www.plip.com','','','','iDo','ma description','mon contenu é&<>','www.plop.com/macat/','ma categorie'); //Ecrit le flux dans un fichier $a->ShowRSS('a.txt')
Paramètre :
Paramètre :
Paramètre :
Paramètre :
Paramètre :
Si $file précisé, ShowRSS renverra true ou false (selon le résultat de la tentative d'écriture du fichier)
Si $file est omis, ShowRSS renverra le contenu du flux.
Il reste une dernière fonction _E. Elle ne nécessite aucun appel. Elle permet d'encoder les données dans le bon format.
Si tous les paramètre de toutes les fonction sont optionnels, certain (en cas d'omission) prendrons des valeurs par défaut.
C'est valeur sont paramétrées dans la fonction MakeRSS :
$this->Ref['encoding'] ='ISO-8859-15'; $this->Ref['protocole'] ='http://'; $this->Ref['langue'] ='fr'; $this->Ref['pubdate'] =date("D, j M Y H:i:s O"); $this->Ref['itemdate'] =date('Y-m-d').'T'.date('H:i:s').'Z'; $this->Ref['format'] ='text/html';
Voici le détail :
— iDo 20/04/2006 11:41
Discussion