begin process at 2012 05 27 17:58:39
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > [PHP5]GENERATION DE FLUX RSS

[PHP5]GENERATION DE FLUX RSS


 Information sur la source

Note :
7 / 10 - par 1 personne
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Class et Objet ( POO ) Classé sous :xml, rss, feed, flux, generateur Niveau :Initié Date de création :19/03/2006 Vu / téléchargé :6 816 / 361

Auteur : hametsu21

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
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

  • <?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>';
  • }
  • ?>
<?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)

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

[PHP5]CLASSE DE SESSION
Source avec Zip [PHP5]CLASSE DE TEMPLATE PHP AVEC CACHE
Source avec Zip SYSTEME DE NOUVELLE POO

 Sources de la même categorie

Source avec Zip GÉNÉRATION AUTOMATIQUE DE FICHIER .CLASS.PHP EN FONCTION D'U... par ig3
CLASSE D'OBJET DE CRYPTAGE ET DÉCRYPTAGE DE CHAINES DE CARAC... par 8Tnerolf8
Source avec Zip MY.DEVIANTART API par inwebo
CLASSE DE GESTION DE "VARIABLES GLOBALES D'ENVIRONNEMENT" par pifou25
Source avec Zip COLLECTION.CLASS.MIN.PHP par thunderhunter

 Sources en rapport avec celle ci

Source avec Zip [PHP5][CLASSE] GÉNÉRATION DE FLUX RSS 2.0 par webdeb
Source avec Zip [PHP5/DOM] CLASSE DE CREATION DE FLUX RSS VIA UN FICHIER XML par jean84
EXPLOITER UN FLUX RSS DANS UNE FONCTION PHP PORTABLE. par R0_0T
Source avec Zip WD_RSS_GENERATOR par webdeb
Source avec Zip BIBLIOTHÈQUE GÉNÉRATRICE DE FLUX RSS par pouda

Commentaires et avis

Commentaire de FhX le 19/03/2006 23:59:39

le closefield() devrait être detecté au moment de l'affichage !
Donc via le __toString() en cas d'oubli !

Commentaire de wizad le 20/03/2006 00:16:07

euh... pourquoi mettre 439 ligne comme ça surtout avec firefox qui inclut les numéros de ligne au copier collé. Ce serait sympa d'avoir au moins un fichier zip avec la source.

Commentaire de Arnauti le 20/03/2006 08:11:48

Wizad -> utilise IE juste pour copier le code.
A++

Commentaire de malalam le 21/03/2006 09:15:09 administrateur CS

Hello Hametsu,

moi ce que je reprocherais principalement à ta source, c'est de ne pas utiliser DOMXML.
Tu aurais très bien pu créer une classe qui étends DOMXML,  en utilisant les spécificités de la (des) norme RSS : addTitle () etc...ça aurait été plus clair en plus. Et tu aurais ainsi pu ajouter des fonctionnalités propres au DOM.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Recherche de flux xml ou/et rss de prog tv sportif [ par bobganjx ] Pauvre de moi en stage, exploit&#233; par un employeur sans scrupule qui me paye pas (c'est beaux les &#233;tudes) ;<font color="#800080" flux rss [ par atchoumen ] Bonjour,je suis en train de mettre un flux rss en place sur mon site et je me retrouve face à un problème bizarre. Lorsque je fais une requete sans co widget+rss+php [ par lsamsoumal ] Bonjour tt le monde: j'ai un code php qui génére un fichier xml contenant des flux rss.Maintenant je veut developper un widget qui récupére les flux r widget avec flux rss [ par pandouta08 ] j'ai un code php d'un générateur d'un fichier xml contenant un ensemble de lien et j'ai un widget yahoo que j'ai créé ,je veux que mon widget yahoo af flux rss [ par mahamourta ] bonsoir tout le monde je veux installer un flux rss sur mon site web . J'ai reussi a avoir un code dans mon fichier xml ,mais je n'arrive pas a l'exp je n arrive pas a metre mon flux rss a jour [ par dinadina83 ] que dieu vous benisse xml et fusionchart [ par yatoo ] Bonjour à tous , Je vous contact car j'aimerais créer un graphique avec mon flux xml et fusionchart.( http://www.fusioncharts.com/ ) Mon flux se com insertion des flux RSS dans des pages php [ par hadjiphp ] Bonjour, merci pour ce site et souhaite de trouver tous ce que je veux. je vous remercie.je suis entrain de créer un site web en php, et je souhai Flux RSS [ par hadjiphp ] Bonjour, merci pour ce site et souhaite de trouver tous ce que je veux. je vous remercie. Bonjour, j'ai une question : comment je peut insérer u probleme de flux xml [ par stomy95 ] Bonjour ,j'ai un souci pour afficher du flux xml  après 2 jours de recherche j'ai toujours pas résolu mon problème sa m'affiche cette phrase :Ce fichi


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,874 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales