PHP – Class – De l’importance du Flux RSS
Le Flux RSS est rentré dans les mœurs des internautes depuis un petit moment déjà, mais aujourd’hui une majeure partie des néophytes savent ce qu’est un Flux RSS et surtout l’utilisent. Sans compter les internautes assidus qui l’utilisent couramment pour se tenir informé.
Il est donc important, voir indispensable d’avoir un flux rss sur son propre site, surtout si ce dernier propose des actualités ou des rentrées de contenus qui peuvent intéresser les lecteurs du site. C’est un atout non négligeable et vraiment utile pour les internautes qui resteront ainsi connecté à votre site via un agrégateur de flux rss. C’est un moyen de plus de fidéliser vos lecteurs.
Ce n’est pas pour rien que la majorité des cms récents proposent du flux rss d’office. Dans un premier temps parce que ce n’est pas le plus complexe à mettre en place (pour un système basique), et parce que ça a forcément son utilité !
Quelques exemples, sans ordre, ni préférence, de ces cms : wordpress, joomla, drupal, phpbb, spip, bilboplanet (agrégateur de flux rss), dotclear, pluxml etc…
En agrégateur, il existe multitude de moyens de se tenir informé via RSS, mais pour les plus utilisés on retrouve tout simplement les navigateurs (firefox, opéra, safari, chrome, ie etc…), google reader, netvibes etc…
De plus le rss est un bon moyen d’envoyer nos actualités sur les réseaux sociaux (facebook, twitter, friendfeed, viadeo etc…), alors pourquoi l’éviter ?
Pour cela je vous propose une petite classe php toute simple pour gérer vos flux rss :
[pastacode lang= »php » manual= »%3C%3Fphp%20%0A%2F**%20%0A*%20%40package%20Ludik_FrameWork%20%0A*%20%40subpackage%20Ludik_FrameWork_1.0%20-%20Evolucion%20%0A*%20%40author%20Franck%20Pertegas%20%0A*%20%40copyright%20LudiKreation%20%0A*%20%40link%20http%3A%2F%2Fwww.ludikreation.com%20%0A*%20%40license%20http%3A%2F%2Fwww.gnu.org%2Flicenses%2Flgpl.html%20GNU%20General%20Lesser%20Public%20Licence%2C%20see%20LICENCE%20file%20%0A*%20%40divers%20Issu%20du%20d%C3%A9veloppement%20pour%20des%20sites%20%3A%3A%20ludikreation%20de%20Franck%20Pertegas%20%0A*%20%40version%201.0.0%20%0A*%20%40Date%2022%2F03%2F2010%20%0A*%2F%20%0A%0A%2F**%20%0A*%20ludikrss%20%0A*%20Classe%20(plugin)%20de%20gestion%20des%20fluxrss%20%0A*%2F%20%0A%0Aclass%20ludikrss%7B%20%0A%09%0A%09%2F**%20%0A%09*%20L’encodage%20du%20flux%20%0A%09*%20%40var%20string%20%0A%09*%20%40access%20private%20%0A%09*%2F%20%0A%09private%20%24encoding%3B%20%0A%09%0A%09%2F**%20%0A%09*%20Le%20titre%20du%20flux%20%0A%09*%20%40var%20string%20%0A%09*%20%40access%20private%20%0A%09*%2F%20%0A%09private%20%24title%3B%20%0A%09%0A%09%2F**%20%0A%09*%20Le%20lien%20du%20site%20du%20flux%20%0A%09*%20%40var%20string%20%0A%09*%20%40access%20private%20%0A%09*%2F%20%0A%09private%20%24link%3B%20%0A%09%0A%09%2F**%20*%20Description%20du%20flux%20%0A%09*%20%40var%20string%20%0A%09*%20%40access%20private%20*%2F%20%0A%09private%20%24description%3B%20%0A%09%0A%09%2F**%20%0A%09*%20La%20langue%20utilis%C3%A9e%20%0A%09*%20%40var%20string%20%0A%09*%20%40access%20private%20%0A%09*%2F%20%0A%09private%20%24lang%3B%20%0A%09%0A%09%2F**%20%0A%09*%20La%20concat%C3%A9nation%20des%20items%20%0A%09*%20%40var%20string%20%0A%09*%20%40access%20private%20%0A%09*%2F%20%0A%09private%20%24items%3B%20%0A%09%0A%09%2F**%20%0A%09*%20Constructeur%20%0A%09*%20Initialisation%20des%20variables%20%0A%09*%20%40param%20string%20%24title%20%3A%3A%20Le%20titre%20du%20flux%20%0A%09*%20%40param%20string%20%24link%20%3A%3A%20Le%20lien%20du%20site%20du%20flux%20%0A%09*%20%40param%20string%20%24description%20%3A%3A%20une%20description%20du%20flux%20(default%3D’Flux%20RSS’)%20%0A%09*%20%40param%20string%20%24lang%20%3A%3A%20La%20langue%20du%20flux%20(default%3D’fr’)%20%0A%09*%20%40param%20string%20%24encoding%20%3A%3A%20L’encodage%20du%20flux%20(default%3D’UTF-8′)%20%0A%09*%20%40return%20void%20*%2F%20%0A%09public%20function%20__construct(%24title%2C%20%24link%2C%20%24description%20%3D%20’Flux%20RSS’%2C%20%24lang%20%3D%20’fr’%2C%20%24encoding%20%3D%20’UTF-8′)%7B%20%0A%09%09%0A%09%09%24this-%26gt%3Bitems%20%3D%20 »%3B%0A%09%09%24this-%26gt%3Btitle%20%3D%20%24title%3B%0A%09%09%24this-%26gt%3Blink%20%3D%20%24link%3B%0A%09%09%24this-%26gt%3Bdescription%20%3D%20%24description%3B%0A%09%09%24this-%26gt%3Blang%20%3D%20%24lang%3B%0A%09%09%24this-%26gt%3Bencoding%20%3D%20%24encoding%3B%0A%09%0A%09%7D%0A%09%0A%09%2F**%26lt%3Bbr%20%3F–%3E%20%0A%09*%20M%C3%A9thode%20static%20private%20(escape_cdata(%5B…%5D))%20Converts%20the%20CDATA%20end%20sequence%20%5D%5D%26gt%3B%20into%20%5D%5D%26gt%3B%0A%09*%20%40param%20string%20%24str%20%3A%3A%20la%20chaine%20%C3%A0%20traiter%0A%09*%20%40return%20%24string%0A%09*%2F%0A%09private%20static%20function%20escape_cdata(%24str)%7B%0A%0A%09%09return%20str_replace(‘%5D%5D%26gt%3B’%2C%20’%5D%5D%26gt%3B’%2C%20%24str)%3B%0A%0A%09%7D%0A%0A%09%2F**%0A%09*%20M%C3%A9thode%20public%20static%20(pun_htmlspecialchars(%5B…%5D))%20Equivalent%20to%20htmlspecialchars()%2C%20but%20allows%20%26amp%3B%23%5B0-9%5D%2B%20(for%20unicode)%0A%09*%20%40param%20string%20%24str%20%3A%3A%20la%20chaine%20%C3%A0%20traiter%0A%09*%20%40return%20%24string%0A%09*%2F%0A%09private%20static%20function%20htmlSpecialChars(%24str)%7B%0A%0A%09%09%24str%20%3D%20preg_replace(‘%2F%26amp%3B(%3F!%23%5B0-9%5D%2B%3B)%2Fs’%2C%20’%26amp%3B’%2C%20%24str)%3B%0A%09%09%24str%20%3D%20str_replace(array(‘%26lt%3B’%2C%20’%26gt%3B’%2C%20’%22’)%2C%20array(‘%26lt%3B’%2C%20’%26gt%3B’%2C%20’%22′)%2C%20%24str)%3B%0A%0A%09%09return%20%24str%3B%0A%0A%09%7D%0A%0A%09%2F**%0A%09*%20M%C3%A9thode%20public%20(addItem(%5B…%5D))%20ajoute%20des%20items%20au%20flux%0A%09*%20%40param%20string%20%24title%20%3A%3A%20balise%20title%0A%09*%20%40param%20string%20%24link%20%3A%3A%20balise%20link%0A%09*%20%40param%20string%20%24description%20%3A%3A%20balise%20description%0A%09*%20%40param%20string%20%24pubdate%20%3A%3A%20ajoute%20pubdate%20%C3%A0%20la%20balise%20description%20(default%20%3D%20 »)%0A%09*%20%40param%20string%20%24auteur%20%3A%3A%20ajoute%20auteur%20%C3%A0%20la%20balise%20description%20(default%20%3D%20 »)%0A%09*%20%40param%20string%20%24content%20%3A%3A%20ajoute%20du%20contenu%20complet%20(img%20etc%20…)%20%C3%A0%20la%20balise%20content%3Aencoded%20(default%20%3D%20 »)%0A%09*%20%40param%20array%20%24array_categories%20%3A%3A%20un%20tableau%20de%20cat%C3%A9gories%20li%C3%A9es%20%C3%A0%20l’article%20(default%20%3D%20NULL)%0A%09*%20%40param%20string%20%24comment_link%20%3A%3A%20lien%20vers%20les%20commentaires%20de%20l’%C3%A9l%C3%A9ment%20(default%20%3D%20 »)%0A%09*%20%40return%20%24string%0A%09*%2F%0A%09public%20function%20addItem(%24title%2C%20%24link%2C%20%24description%2C%20%24pubdate%20%3D%20 »%2C%20%24auteur%20%3D%20 »%2C%20%24content%20%3D%20 »%2C%20%24array_categories%20%3D%20NULL%2C%20%24comment_link%20%3D%20 »)%7B%0A%0A%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%22. ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22. ».self%3A%3AhtmlSpecialChars(%24title). ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22. ».%24link. ».%22%5Cr%5Cn%22%3B%0A%0A%09%09if(%24comment_link%20!%3D%20 »)%0A%09%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22.’Lien%20vers%20les%20commentaires’.%22%5Cr%5Cn%22%3B%0A%0A%09%09if(%24pubdate%20!%3D%20 »)%0A%09%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22. ».date(‘r’%2C%20%24pubdate). ».%22%5Cr%5Cn%22%3B%0A%0A%09%09if(%24auteur%20!%3D%20 »)%0A%09%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22.self%3A%3Aescape_cdata( ».%24auteur. »).%22%5Cr%5Cn%22%3B%0A%0A%09%09if(%24array_categories%20!%3D%20NULL%20AND%20is_array(%24array_categories))%7B%0A%0A%09%09%09foreach(%24array_categories%20as%20%24cle%3D%26gt%3B%24valeur)%7B%0A%0A%09%09%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22.’%3C!%5BCDATA%5B’.self%3A%3Aescape_cdata(%24valeur).’%5D%5D%3E’.%22%5Cr%5Cn%22%3B%0A%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22.’%3C!%5BCDATA%5B’%3B%20if(%24auteur%20!%3D%20 »)%20%24this-%3Eitems%20.%3D%20self%3A%3Aescape_cdata(‘Auteur%20%3A%20′.%24auteur. »)%3B%0A%0A%09%09if(%24pubdate%20!%3D%20 »)%0A%09%09%09%24this-%3Eitems%20.%3D%20self%3A%3Aescape_cdata(%22%5Cr%5Cn%22.’Ecrit%20le%20%3A%20’.date(‘r’%2C%20%24pubdate). »)%3B%0A%0A%09%09%24this-%3Eitems%20.%3D%20self%3A%3Aescape_cdata(%24description)%3B%0A%0A%09%09%24this-%3Eitems%20.%3D%20’%5D%5D%3E’.%22%5Cr%5Cn%22%3B%0A%0A%09%09if(%24content%20!%3D%20 »)%0A%09%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%5Ct%22.’%3C!%5BCDATA%5B’.self%3A%3Aescape_cdata(%24content).’%5D%5D%3E’.%22%5Cr%5Cn%22%3B%0A%0A%09%09%24this-%26gt%3Bitems%20.%3D%20%22%5Ct%22. ».%22%5Cr%5Cn%22%3B%0A%0A%09%09return%20%24this-%26gt%3Bitems%3B%0A%0A%09%7D%0A%0A%09%2F**%0A%09*%20M%C3%A9thode%20public%20(headerFlux())%20ajoute%20le%20header%20au%20flux%0A%09*%20%40return%20void%0A%09*%2F%0A%09public%20function%20headerFlux()%7B%0A%0A%09%09%2F%2F%20Send%20XML%2Fno%20cache%20headers%0A%09%09header(‘Content-Type%3A%20text%2Fxml’)%3B%0A%09%09header(‘Expires%3A%20’.gmdate(‘D%2C%20d%20M%20Y%20H%3Ai%3As’).’%20GMT’)%3B%0A%09%09header(‘Cache-Control%3A%20must-revalidate%2C%20post-check%3D0%2C%20pre-check%3D0’)%3B%0A%09%09header(‘Pragma%3A%20public’)%3B%0A%0A%09%7D%0A%0A%09%2F**%0A%09*%20M%C3%A9thode%20public%20(sendFlux())%20ajoute%20le%20flux%0A%09*%20%40return%20string%0A%09*%2F%0A%09public%20function%20sendFlux()%7B%0A%0A%09%09%2F%2F%20It’s%20time%20for%20some%20syndication!%0A%09%09%24out%20%3D%20’%3C!–%3Fxml%20version%3D%221.0%22%20encoding%3D%22′.%24this-%26gt%3Bencoding.’%22%3F–%3E’.%22%5Cr%5Cn%22%3B%0A%09%09%2F%2F%20%24out%20.%3D%20 ».%22%5Cr%5Cn%22%3B%0A%09%09%2F%2F%24out%20.%3D%20 ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20’%0A%09%09’%3B%0A%0A%09%09%24out%20.%3D%20 ».%22%5Cr%5Cn%22%3B%0A%09%09%24out%20.%3D%20%22%5Ct%22. ».self%3A%3AhtmlSpecialChars(%24this-%26gt%3Btitle). ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20%22%5Ct%22. ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20%22%5Ct%22. ».%24this-%26gt%3Blink.’%2F’.%22%5Cr%5Cn%22%3B%0A%09%09%24out%20.%3D%20%22%5Ct%22. ».%24this-%26gt%3Bdescription. ».%22%5Cr%5Cn%22%3B%0A%09%09%24out%20.%3D%20%22%5Ct%22. ».date(‘r’%2C%20time()). ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20%22%5Ct%22.’http%3A%2F%2Fwww.ludikreation.com’.%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20%22%5Ct%22. ».%24this-%26gt%3Blang. ».%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20%22%5Ct%22.’hourly’.%22%5Cr%5Cn%22%3B%0A%09%09%24out%20.%3D%20%22%5Ct%22.’1′.%22%5Cr%5Cn%22%3B%0A%0A%09%09%24out%20.%3D%20%24this-%26gt%3Bitems%3B%0A%0A%09%09%24out%20.%3D%20 ».%22%5Cr%5Cn%22%3B%0A%09%09%24out%20.%3D%20’%20’%3B%0A%0A%09%09return%20%24out%3B%0A%0A%09%7D%0A%0A%09%2F**%0A%09*%20M%C3%A9thode%20public%20(writeFlux())%20%C3%A9crit%20le%20flux%0A%09*%20%40return%20string%0A%09*%2F%0A%09public%20function%20writeFlux()%7B%0A%0A%09%09%24this-%26gt%3BheaderFlux()%3B%0A%0A%09%09echo%20%24this-%26gt%3BsendFlux()%3B%0A%0A%09%7D%0A%0A%7D%0A%0A%3F%3E » message= » » highlight= » » provider= »manual »/]
Son utilisation reste simple :
[pastacode lang= »php » manual= »%3C%3Fphp%0A%2F%2F%20APPEL%20DE%20LA%20CLASS%0Ainclude(‘class%2Frss.class.php’)%3B%0A%0A%2F%2F%20Instanciation%20pour%20la%20future%20sortie%20du%20rss%0A%24rss%20%3D%20new%20ludikrss(‘Actualit%C3%A9%20LudiKreation’%2C%20’http%3A%2F%2Fwww.ludikreation.com’)%3B%0A%0A%24result%20%3D%20mysql_db_query(%22database%22%2C%20%22select%20*%20from%20actu%22)%3B%0A%0Awhile(%24row%20%3D%20mysql_fetch_array(%24result))%20%7B%0A%0A%24rss-%3EaddItem(%20%24row%5B’title’%5D%2C%0A’http%3A%2F%2Fwww.ludikreation.com%2Farticles%2Fview.php%3Fid%3D’.%24row%5B%22id_article%22%5D%2C%0Asubstr(%24row%5B’content’%5D%2C%20200)%2C%0A%24row%5B’pubdate’%5D%2C%0A’LudiKreation’%2C%0A%24row%5B’content’%5D%2C%0Aarray(‘Web’%2C%20’Internet’%2C%20’R%C3%A9f%C3%A9rencement’))%3B%0A%0A%7D%0A%24rss-%3EwriteFlux()%3B%0Aexit()%3B%0A%3F%3E » message= » » highlight= » » provider= »manual »/]
Et vous avez une sortie rss propre.
Bien entendu cette classe PHP reste améliorable et modifiable, je reste à votre écoute et accepte avec plaisir les remarques et améliorations 🙂
Bon code à tous !
EDIT DU 26.09.2010
Pour compléter cet article, voilà l’explication sur l’utilisation de cette classe (des notions de php sont nécessaires) :
1/ Incluez le fichier de la classe sur la page ou vous souhaitez l’utiliser.
2/ Créer une instance de ludikrss, comme suis :
[pastacode lang= »php » manual= »%24rss%20%3D%20new%20ludikrss(‘Titre%20de%20mon%20flux%20rss’%2C%20’http%3A%2F%2Fliendemonsite.fr’)%3B » message= » » highlight= » » provider= »manual »/]
3/ Créez la requête qui ira chercher vos données dans la BDD.
4/ Créez une boucle qui permettra de récupérer les sorties de la bdd, et utiliser addItem de la classe, comme ceci :
[pastacode lang= »php » manual= »%3C%3Fphp%0A%24rss-%3EaddItem(%20’Titre%20de%20mon%20article’%2C%0A’http%3A%2F%2Flienversmonarticle.com%3Farticle%3Did’%2C%0A’Une%20courte%20description%20de%20l%5C’article…’%2C%0A’Date%20de%20publication%20au%20format%20mktime’%2C%0A’Auteur’%2C%0A’Contenu%20de%20l%5C’article’%2C%0Aarray(‘un’%2C’tableau’%2C’des’%2C’diff%C3%A9rentes’%2C’cat%C3%A9gories’%2C’de’%2C’l%5C’article’))%3B%0A%3F%3E » message= » » highlight= » » provider= »manual »/]
5/ Une fois que vous avez rajouté tous les éléments que vous souhaitiez, vous pouvez afficher la sortie :
[pastacode lang= »php » manual= »%3C%3Fphp%0A%24rss-%3EwriteFlux()%3B%0Aexit()%3B%0A%3F%3E » message= » » highlight= » » provider= »manual »/]
Articles pouvant vous intéresser :
25 septembre 2010
Merci, pourrions nous avoir un petit process pour compléter ces deux fichiers svp ?
merci
26 septembre 2010
Salut DC,
J’ai rajouté un edit en fin d’article, en espérant éclaircir l’utilisation de cette petite classe php 😉
++
27 septembre 2010
Hello LudiKadmin,
Grand merci 🙂