[php5]generation de flux rss

Description

Petite classe permettant la génération de flux RSS statique (feed.rss) ou dynamique (fichier php générant le flux mais qui execute une requête, donc imaginer le nombre de requête si tous vos membres veulent accéder à votre flux...)

je ne connais pas bien le langage XML et la POO, c'est récent pour moi, merci de m'aiguiller si j'ai fait des erreurs ( c'est même sûr !)

Oublier pas d'indiquer l'adresse de votre flux :

<a type="application/rss+xml" href="feed.xml> Flux RSS de cette page</a>

Source / Exemple :


<?php
/**

  • RSS200 Object.
*
  • @name RSS200
  • @since 1.0
*
  • @author Berlemont Quentin
  • @copyright 2006 Berlemont Quentin
*
  • /
class RSS200 { /**
  • @desc RSS property
*
  • @access private
  • @since 1.0
  • @type array
  • /
private $RSSarr = array('encoding' => '', 'stylesheet' => '', 'version' => '', 'title' => '', 'link' => '', 'description' => '', 'language' => '', 'copyright' => '', 'managingEditor' => '', 'webMaster' => '', 'pubDate' => '', 'lastBuildDate' => '', 'category' => array ('category' => '', 'domain' => ''), 'generator' => 'RSS generated by Quentin Berlemont\'s RSS200 Class', 'docs' => 'http://blogs.law.harvard.edu/tech/rss', 'cloud' => array('domain' => '', 'port' => '', 'path' => '', 'registerProcedure' => '', 'protocol' => ''), 'ttl' => '', 'image' => array('url' => '', 'title' => '', 'link' => '', 'width' => '88', 'height' => '31'), 'rating' => '', 'textInput' => array('title' => '', 'description' => '', 'name' => '', 'link' => ''), 'skipHours' => array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16, 17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22, 23 => 23), 'skipDays' => array('Monday' => 'Monday', 'Tuesday' => 'Tuesday', 'Wednesday' => 'Wednesday', 'Thursday' => 'Thursday', 'Friday' => 'Friday', 'Saturday' => 'Saturday', 'Sunday' => 'Sunday'), 'item' => array('title' => '', 'link' => '', 'description' => '', 'author' => '', 'category' => array ('category' => '', 'domain' => ''), 'comments' => '', 'enclosure' => array('url' => '', 'length' => '', 'type' => ''), 'guid' => array('guid' => '', 'isPermaLink' => ''), 'pubDate' => '', 'source' => array('source' => '', 'url' => '') ) ); /**
  • @desc RSS Buffer
*
  • @access private
  • @since 1.0
  • @type string
  • /
private $RSSbuffer; /**
  • @desc RSS Save Path
*
  • @access private
  • @since 1.0
  • @type string
  • /
private $RSSpath; /**
  • @desc RSS Filename
*
  • @access private
  • @since 1.0
  • @type string
  • /
private $RSSname; /**
  • @desc Create a new RSS200 Object instance.
*
  • @param string A RssPath
  • @param string A RssName
*
  • @access public
  • @since 1.0
  • /
public function __construct($RssPath, $RssName) { if(is_dir($RssPath)) $this -> RSSpath = $RssPath; $this -> RSSname = $RssName . '.xml'; } /**
  • @desc Open a RSS Feed.
*
  • @param array A ArgumentsArr
*
  • @access public
  • @since 1.0
  • /
public function OpenFeed($ArgumentsArr = array ()) { foreach ($this -> RSSarr as $key => $value) { if (array_key_exists ($key, $ArgumentsArr)) { $this -> RSSarr[$key] = $ArgumentsArr[$key]; } else if (!empty ($value)) { $this -> RSSarr[$key] = $value; } } $this -> RSSbuffer = '<?xml version="1.0" encoding="' . $this -> RSSarr['encoding'] . '" ?>'; if (!empty($this -> RSSarr['stylesheet'])) $this -> RSSbuffer .= '<?xml-stylesheet type="text/xsl" title="RSS" href="' . $this -> RSSarr['stylesheet'] . '"?>'; $this -> RSSbuffer .= '<rss version="' . $this -> RSSarr['version'] . '">'; $this -> RSSbuffer .= ' <channel>'; $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['title'] . '</title>'; $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['link'] . '</link>'; $this -> RSSbuffer .= ' <description>' . $this -> RSSarr['description'] . '</description>'; if (!empty($this -> RSSarr['language'])) $this -> RSSbuffer .= ' <language>' . $this -> RSSarr['language'] . '</language>'; if (!empty($this -> RSSarr['copyright'])) $this -> RSSbuffer .= ' <copyright>' . $this -> RSSarr['copyright'] . '</copyright>'; if (!empty($this -> RSSarr['managingEditor'])) $this -> RSSbuffer .= ' <managingEditor>' . $this -> RSSarr['managingEditor'] . '</managingEditor>'; if (!empty($this -> RSSarr['webMaster'])) $this -> RSSbuffer .= ' <webMaster>' . $this -> RSSarr['webMaster'] . '</webMaster>'; if (!empty($this -> RSSarr['pubDate'])) $this -> RSSbuffer .= ' <pubDate>' . $this -> RSSarr['pubDate'] . '</pubDate>'; if (!empty($this -> RSSarr['lastBuildDate'])) $this -> RSSbuffer .= ' <lastBuildDate>' . $this -> RSSarr['lastBuildDate'] . '</lastBuildDate>'; if (!empty($this -> RSSarr['category'])) $this -> RSSbuffer .= ' <category domain="' . $this -> RSSarr['category']['domain'] . '">' . $this -> RSSarr['category']['category'] . '</category>'; if (!empty($this -> RSSarr['generator'])) $this -> RSSbuffer .= ' <generator>' . $this -> RSSarr['generator'] . '</generator>'; if (!empty($this -> RSSarr['doc'])) $this -> RSSbuffer .= ' <doc>' . $this -> RSSarr['doc'] . '</doc>'; if (!empty($this -> RSSarr['cloud'])) $this -> RSSbuffer .= ' <cloud domain="' . $this -> RSSarr['cloud']['domain'] . '" port="' . $this -> RSSarr['cloud']['port'] . '" path="' . $this -> RSSarr['cloud']['path'] . '" registerProcedure="' . $this -> RSSarr['cloud']['registerProcedure'] . '" protocol="' . $this -> RSSarr['cloud']['protocol'] . '"/>'; if (!empty($this -> RSSarr['ttl'])) $this -> RSSbuffer .= ' <ttl>' . $this -> RSSarr['ttl'] . '</ttl>'; if (!empty($this -> RSSarr['image'])) { $this -> RSSbuffer .= ' <image>'; if (!empty($this -> RSSarr['image']['url'])) $this -> RSSbuffer .= ' <url>' . $this -> RSSarr['image']['url'] . '</url>'; if (!empty($this -> RSSarr['image']['title'])) $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['image']['title'] . '</title>'; if (!empty($this -> RSSarr['image']['link'])) $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['image']['link'] . '</link>'; if (!empty($this -> RSSarr['image']['width'])) $this -> RSSbuffer .= ' <width>' . $this -> RSSarr['image']['width'] . '</width>'; if (!empty($this -> RSSarr['image']['height'])) $this -> RSSbuffer .= ' <height>' . $this -> RSSarr['image']['height'] . '</height>'; $this -> RSSbuffer .= ' </image>'; } //if (!empty($this -> RSSarr['rating'])) Comment marche cette proriete ? //$this -> RSSbuffer .= ' <rating>' . $this -> RSSarr['rating'] . '</rating>'; if (!empty($this -> RSSarr['textInput'])) { $this -> RSSbuffer .= ' <textInput>'; if (!empty($this -> RSSarr['textInput']['title'])) $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['textInput']['title'] . '</title>'; if (!empty($this -> RSSarr['textInput']['description'])) $this -> RSSbuffer .= ' <description>' . $this -> RSSarr['textInput']['description'] . '</description>'; if (!empty($this -> RSSarr['textInput']['name'])) $this -> RSSbuffer .= ' <name>' . $this -> RSSarr['textInput']['name'] . '</name>'; if (!empty($this -> RSSarr['textInput']['link'])) $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['textInput']['link'] . '</link>'; $this -> RSSbuffer .= ' </textInput>'; } if (!empty($this -> RSSarr['skipHours'])) $this -> RSSbuffer .= ' <hour>' . $this -> RSSarr['skipHours'] . '</hour>'; if (!empty($this -> RSSarr['skipDays'])) $this -> RSSbuffer .= ' <day>' . $this -> RSSarr['skipDays'] . '</day>'; } /**
  • @desc Add item in the RSS Feed.
*
  • @param array A element
  • @param array A ArgumentsArr
*
  • @access public
  • @since 1.0
  • /
public function AddItem($element, $ArgumentsArr) { if (!array_key_exists ($element, $this -> RSSarr)) throw new Exception ($element . ' n\'est pas un élément RSS valide'); foreach ($this -> RSSarr[$element] as $key => $value) { if (array_key_exists ($key, $ArgumentsArr)) $this -> RSSarr[$element][$key] = $ArgumentsArr[$key]; else if (!empty ($value)) $this -> RSSarr[$element][$key] = $value; } if (!empty($this -> RSSarr['item'])) { $this -> RSSbuffer .= ' <item>'; if (!empty($this -> RSSarr['item']['title'])) $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['item']['title'] . '</title>'; if (!empty($this -> RSSarr['item']['link'])) $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['item']['link'] . '</link>'; if (!empty($this -> RSSarr['item']['description'])) $this -> RSSbuffer .= ' <description>' . $this -> RSSarr['item']['description'] . '</description>'; if (!empty($this -> RSSarr['item']['author'])) $this -> RSSbuffer .= ' <author>' . $this -> RSSarr['item']['author'] . '</author>'; if (!empty($this -> RSSarr['item']['category'])) $this -> RSSbuffer .= ' <category domain="' . $this -> RSSarr['item']['category']['domain'] . '">' . $this -> RSSarr['item']['category']['category'] . '</category>'; if (!empty($this -> RSSarr['item']['comment'])) $this -> RSSbuffer .= ' <comments>' . $this -> RSSarr['item']['comments'] . '</comments>'; if (!empty($this -> RSSarr['item']['enclosure'])) $this -> RSSbuffer .= ' <enclosure url="' . $this -> RSSarr['item']['enclosure']['url'] . '" length="' . $this -> RSSarr['item']['enclosure']['length'] . '" type="' . $this -> RSSarr['item']['enclosure']['type'] . '" />'; if (!empty($this -> RSSarr['item']['guid'])) $this -> RSSbuffer .= ' <guid isPermaLink="' . $this -> RSSarr['item']['guid']['isPermaLink'] . '">' . $this -> RSSarr['item']['guid']['guid'] . '</guid>'; if (!empty($this -> RSSarr['item']['pubDate'])) $this -> RSSbuffer .= ' <pubDate>' . $this -> RSSarr['item']['pubDate'] . '</pubDate>'; if (!empty($this -> RSSarr['item']['source'])) $this -> RSSbuffer .= ' <source url="' . $this -> RSSarr['item']['source']['url'] . '">' . $this -> RSSarr['item']['source']['source'] . '</source>'; $this -> RSSbuffer .= ' </item>'; } } /**
  • @desc Close RSS Feed.
*
  • @access public
  • @since 1.0
  • /
public function CloseFeed() { $this -> RSSbuffer .= '</channel>'; $this -> RSSbuffer .= '</rss>'; } /**
  • @desc Save RSS Feed in a XML file.
*
  • @access public
  • @since 1.0
  • /
public function SaveFeed() { if (@is_file($this -> RSSpath . $this -> RSSname)) @unlink($this -> RSSpath . $this -> RSSname); $fopen = @fopen($this -> RSSpath . $this -> RSSname, 'w+'); if (!$fopen) throw new Exception('Impossible d\'ouvrir un fichier' . $this -> RSSpath . $this -> RSSname); fwrite($fopen, $this -> RSSbuffer); fclose($fopen); } /**
  • @desc Show RSS Feed.
*
  • @return string RSSbuffer content the RSS feed
*
  • @access public
  • @since 1.0
  • /
public function __toString() { header('Content-type: text/xml'); return $this -> RSSbuffer; } /**
  • @desc destruct RSS200 Object instance.
*
  • @access public
  • @since 1.0
  • /
public function __destruct () { unset ($this); } } ?> EXEMPLE D UTILISATION <?php include_once('RSS200.class.php'); // try to create RSS Feed. try { $FeedPath = ''; // Directory where the XML file save, WITH '/' at the end. $FeeName = ''; // Name of the RSS Feer, for exemle 'news' NOT 'new.xml' ! // new RSS200 Object instance. $rss = new RSS200($FeedPath, $FeeName); // Open RSS Feed. $rss -> OpenFeed(array( 'encoding' => 'utf-8', 'stylesheet' => '', 'version' => '2.0', 'title' => 'MonSite', 'link' => 'http://www.MonSite.com', 'description' => 'Mon Super site !', 'language' => 'fr-FR', 'copyright' => 'Copyright by Moi', 'managingEditor' => 'Moi', 'webMaster' => 'Moi', 'pubDate' => '', 'lastBuildDate' => '', 'category' => array ('category' => '', 'domain' => ''), 'generator' => 'RSS generated by Quentin Berlemont\'s RSS200 Class', // LESS IT, THANX. 'docs' => 'http://blogs.law.harvard.edu/tech/rss', // FULL DOCUMENTATION ABOUT XML (en) AND http://www.stervinou.com/projets/rss/ (fr). 'cloud' => array('domain' => '', 'port' => '', 'path' => '', 'registerProcedure' => '', 'protocol' => ''), 'ttl' => '', 'image' => array('url' => '', 'title' => '', 'link' => '', 'width' => '88', 'height' => '31'), 'rating' => '', 'textInput' => array('title' => '', 'description' => '', 'name' => '', 'link' => ''), 'skipHours' => '', // Number between 0 and 23. 'skipDays' => '', // Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday. ) ); $rss -> AddItem('item', array('title' => 'test 1', 'link' => '', 'description' => ' description 1', 'author' => '', 'category' => array ('category' => '', 'domain' => ''), 'comments' => '', 'enclosure' => array('url' => '', 'length' => '', 'type' => ''), 'guid' => array('guid' => '', 'isPermaLink' => ''), 'pubDate' => '', 'source' => array('source' => '', 'url' => '') ) ); // Close RSS Feed, DON't FORGET IT ! $rss -> CloseFeed(); echo $rss; // Show RSS Feed. //$rss -> SaveFeed(); // Save RSS Feed in a XML file. } //if Error, show it catch(Exception $e) { echo '<pre>', print_r($e), '</pre>'; } ?>

Conclusion :


Future amelioration, ajouter une méthode pour parser les fichiers XML distant et elle sera complète (je pense)

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.