Logo classes.scriptsphp.org PHP

go to nav bar

RSS 2.00 Beta

Création et parsing de fichier RSS.

La classe RSS 2.00 fournit des fonctions permettant de parser ou de créer des fichiers RSS conformément aux spécifications RSS 2.00. Le parser utilise les fonctions XML de PHP, il est donc impératif que votre document ait une syntaxe XML valide.

Cette classe utilise un wrapper http different de celui de php ainsi qu un systeme de cache, elle necessite donc des classe associées.

La classe est une version de test, normalement fonctionnelle mais pas exempte de bugs.

Les ajout par rapport a la au systeme de base :

  • un wrapper HTTP controlant plus finement les timeout et les erreur de reception des Feeds.
  • un cache HTTP evitant les requete http inutile.
  • la gestion faclité des encodage d'entrée et sortie du parser RSS.

Les Méthodes

Exemples

  1. Parser un fichier RSS - Affichage Simple
  2. Parser un fichier RSS - Affichage avancé
  3. Création d' un fichier RSS Simple
  4. Création d' un fichier RSS à partir d' infos venant de MySQL

La Source

<?php

//error_reporting(0);

function u2i($in,$arg = array()) {
    
$inEnc 'ISO-8859-1';
    
$outEnc 'UTF-8';
    
    if (isset(
$arg['t'])) $outEnc .= '//TRANSLIT';
    if (isset(
$arg['i'])) $outEnc .= '//IGNORE';
    
    if (!isset(
$arg['noSC'])) $in htmlspecialchars($in,ENT_COMPAT,$inEnc);
    
//return iconv('iso-8859-1','utf-8//translit',$in);
    
return iconv($inEnc,$outEnc,$in);
}

require_once(
'getHttp.class.php');
require_once(
'cacheCore.class.php');
require_once(
'gc.class.php');

class 
RSS {
    
    
// parser
    
var $error_log;
    var 
$fp;
    var 
$File;
    var 
$i 0;
    var 
$tag;
    var 
$parent;
    var 
$channel=array();
    var 
$image=array();
    var 
$textinput=array();
    var 
$item=array();
    var 
$skiphours=array();
    var 
$skipdays=array();
    var 
$stylesheet_url;
    var 
$XSL_stylesheet_url;
    
    var 
$cacheTime 600;
    var 
$httpTimeout 4;
    
    var 
$inEnc 'ISO-8859-1';
    var 
$outEnc 'ISO-8859-1';
    
    var 
$infos = array();
    
    
// RSS parser
    
    
function Parser($file) {
        
        
$this->0;
        
        
$this->channel=array(
                                
'TITLE' => '',
                                
'DESCRIPTION' => '',
                                
'LINK' => '',
                                
'LANGUAGE' => '',
                                
'RATING' => '',
                                
'COPYRIGHT' => '',
                                
'PUBDATE' => '',
                                
'LASTBUILDDATE' => '',
                                
'DOCS' => '',
                                
'MANAGINGEDITOR' => '',
                                
'WEBMASTER' => '',
                                
'CATEGORY' => '',
                                
'GENERATOR' => ''
                            
);
        
$this->image=array(
                                
'TITLE' =>'',
                                
'URL' => '',
                                
'LINK' => '',
                                
'WIDTH' => '',
                                
'HEIGHT' => '',
                                
'DESCRIPTION' => ''
                            
);
        
$this->textinput=array(
                                
'TITLE' => '',
                                
'NAME' => '',
                                
'DESCRIPTION' => '',
                                
'LINK' => ''
                            
);
        
        
$this->item = array();
        
$this->skiphours = array( 'HOUR' => '' );
        
$this->skipdays = array( 'DAY' => '' );
        
        
$i=0;
        
$this->File $file;
/*
        if (!$this->fp = @fopen($this->File,"r")) {
            $this->error_log = 'Impossible d ouvrir : '.$this->File;
            return false;
        }
*/
        
    
}
    
    function 
Start($handle$tag$a) {
        
        switch(
$this->parent) {            
        
            case 
'ITEM' :
            
            switch(
$tag) {            
                case 
'ENCLOSURE' :
                case 
'SOURCE' :
                case 
'GUID' :        
                case 
'CATEGORY' :        
                case 
'CC:LICENSE':
                
                    if (
sizeof($a)) {
                            while (list(
$k$v) = each($a)) {
                            
$this->item[$this->i][$tag][$k] = $v;
                        }
                    }
                    
                break;
            }
        
            break;
            
            case 
'CHANNEL' :
            
            switch(
$tag) {
                case 
'CLOUD' :
                case 
'CC:LICENSE':
                                
                    if (
sizeof($a)) {
                            while (list(
$k$v) = each($a)) {
                            
$this->item[$this->i][$tag][$k] = $v;
                        }
                    }
                    
                break;
            }
        }
        
        
$this->parent $this->tag;
        
$this->tag $tag;
        
    }
                
                
        
    function 
End($handle$tag) {
        
        switch(
$this->tag) {
            case 
'IMAGE' :
            case 
'ITEM' :
            case 
'TEXTINPUT' :
            case 
'SKIPHOURS' :
            case 
'SKIPDAYS' :
            
            
$this->parent 'CHANNEL';
            
            break;
        }
        
        if(
$tag === 'ITEM'$this->i++;
        
$this->tag $this->parent;
    }
    
    function 
Data($handle$str) {
        
//$str = trim($str);
        
if($str !== '') {
            switch(
$this->parent) {
                
                case 
'CHANNEL' :
                switch(
$this->tag) {
                    
                    
// 4 required
                    
case 'TITLE':
                    case 
'LINK' :
                    case 
'DESCRIPTION' :
                    case 
'LANGUAGE' :
                    case 
'COPYRIGHT' :
                    case 
'MANAGINGEDITOR' :
                    case 
'WEBMASTER' :
                    
                    case 
'PUBDATE' :
                    case 
'LASTBUILDDATE' :
                    case 
'GENERATOR' :                    
                    case 
'DOCS' :
                    case 
'TTL' :                    
                    case 
'RATING' :

                    
                    
// Dublin Core
                    
case 'DC:TITLE' :      
                    case 
'DC:CREATOR' :
                    case 
'DC:SUBJECT' :
                    case 
'DC:DESCRIPTION' :
                    case 
'DC:PUBLISHER' :
                    case 
'DC:CONTRIBUTOR' :
                    case 
'DC:DATE' :
                    case 
'DC:TIME' :
                    case 
'DC:FORMAT' :
                    case 
'DC:IDENTIFIER' :
                    case 
'DC:SOURCE' :    
                    case 
'DC:LANGUAGE' :
                    case 
'DC:RELATION' :                    
                    case 
'DC:COVERAGE' :
                    case 
'DC:RIGHTS' :            
                    
                    case 
'CREATIVECOMMONS:LICENSE':
                    
                    
$this->channel[$this->tag] .= $str;
                    
                    break;
                    
                    case 
'CLOUD' 
                    case 
'CC:LICENSE':
                        if ( isset( 
$this->item[$this->i][$this->tag] ) ) {
                            
$this->channel[$this->tag]['TEXT'] .= $str
                        } else {
                            
$this->channel[$this->tag]['TEXT'] = $str;
                        }
                        
                    break;
                }
                break;
                
                case 
'IMAGE' :
                switch(
$this->tag) {
                    case 
'TITLE':
                    case 
'URL' :
                    case 
'LINK' :
                    case 
'WIDTH' :
                    case 
'HEIGHT' :
                    case 
'DESCRIPTION' :
                    
                    
// Dublin Core
                    
case 'DC:TITLE' :      
                    case 
'DC:CREATOR' :
                    case 
'DC:SUBJECT' :
                    case 
'DC:DESCRIPTION' :
                    case 
'DC:PUBLISHER' :
                    case 
'DC:CONTRIBUTOR' :
                    case 
'DC:DATE' :
                    case 
'DC:TIME' :
                    case 
'DC:FORMAT' :
                    case 
'DC:IDENTIFIER' :
                    case 
'DC:SOURCE' :    
                    case 
'DC:LANGUAGE' :
                    case 
'DC:RELATION' :                    
                    case 
'DC:COVERAGE' :
                    case 
'DC:RIGHTS' :            
                    
                    
$this->image[$this->tag] .= $str;
                }
                break;
                
                case 
'TEXTINPUT' :
                switch(
$this->tag) {
                    case 
'TITLE' :
                    case 
'DESCRIPTION' :
                    case 
'NAME' :
                    case 
'LINK' :
                    
                    
// Dublin Core
                    
case 'DC:TITLE' :      
                    case 
'DC:CREATOR' :
                    case 
'DC:SUBJECT' :
                    case 
'DC:DESCRIPTION' :
                    case 
'DC:PUBLISHER' :
                    case 
'DC:CONTRIBUTOR' :
                    case 
'DC:DATE' :
                    case 
'DC:TIME' :
                    case 
'DC:FORMAT' :
                    case 
'DC:IDENTIFIER' :
                    case 
'DC:SOURCE' :    
                    case 
'DC:LANGUAGE' :
                    case 
'DC:RELATION' :                    
                    case 
'DC:COVERAGE' :
                    case 
'DC:RIGHTS' :                
                    
                    
$this->textinput[$this->tag] .= $str;
                }
                break;
                
                case 
'ITEM' :
                switch(
$this->tag) {
                    case 
'TITLE' :
                    case 
'DESCRIPTION' :
                    case 
'LINK' :

                    case 
'AUTHOR' :
                    case 
'COMMENTS' :
                     case 
'PUBDATE' 
                      
 
                     
// Dublin Core
                    
case 'DC:TITLE' :      
                    case 
'DC:CREATOR' :
                    case 
'DC:SUBJECT' :
                    case 
'DC:DESCRIPTION' :
                    case 
'DC:PUBLISHER' :
                    case 
'DC:CONTRIBUTOR' :
                    case 
'DC:DATE' :
                    case 
'DC:TIME' :
                    case 
'DC:FORMAT' :
                    case 
'DC:IDENTIFIER' :
                    case 
'DC:SOURCE' :    
                    case 
'DC:LANGUAGE' :
                    case 
'DC:RELATION' :                    
                    case 
'DC:COVERAGE' :
                    case 
'DC:RIGHTS' :    
                    case 
'CONTENT:ENCODED' :
                    case 
'TRACKBACK:PING' :                
                    case 
'TRACKBACK:ABOUT' :        
                    
                    case 
'CREATIVECOMMONS:LICENSE':        
                    
                    if(isset(
$this->item[$this->i][$this->tag]))
                    
$this->item[$this->i][$this->tag] .= $str;
                    else
                    
$this->item[$this->i][$this->tag] = $str;
                    break;
                    
                    case 
'SOURCE' 
                    case 
'CATEGORY' 
                    case 
'ENCLOSURE' 
                    case 
'GUID' :
                    case 
'CC:LICENSE':
                        if ( isset(
$this->item[$this->i][$this->tag]) ) {
                            
$this->item[$this->i][$this->tag]['TEXT'] .= $str;
                        } else {
                            
$this->item[$this->i][$this->tag]['TEXT'] = $str;
                        }
                        
                    break;
                }
                break;
                
                case 
'SKIPHOURS' :
                switch(
$this->tag) {
                    case 
'HOUR' :
                    
$this->skiphours[] = $str;
                }
                break;
                
                case 
'SKIPDAYS' :
                switch(
$this->tag) {
                    case 
'DAY' :
                    
$this->skipdays[] = $str;
                }
                break;
            }
        }
    }
    
    function 
Parse() {
        
$this->xml_parser_create($this->inEnc);
        
xml_set_object($this->x$this);
        
xml_parser_set_option($this->xXML_OPTION_CASE_FOLDINGTRUE);
        
xml_parser_set_option($this->xXML_OPTION_TARGET_ENCODING$this->outEnc);
        
xml_set_element_handler($this->x'Start''End');
        
xml_set_character_data_handler($this->x'Data');
        
        
/* HTTP section */
        
$url = new getHttp($this->File,'GET',$this->cacheTime,$this->httpTimeout);
        
        if (!
$url->readsocket'USE_CACHE' )) {
            
$this->error_log[] = "Impossible d ouvrir le fichier : $file\n";
            return 
FALSE;
        }
        
        if (!
$url->isOK()) {
            
$this->error_log[] = 'erreur HTPP : '.$url->httpErrorStatus;
            return 
FALSE;
        }
        
        
$rawRss = &$url->body;
        
        
/* parsing */
        
$parseState xml_parse($this->x$rawRss);
        
        if (!
$parseState) {
           
$this->error_log[] = 'erreur parsing XML';
            return 
FALSE;
        }
        
        
xml_parser_free($this->x);
        
        
$infos = array(
        
'channel' => $this->channel,
        
'item' => $this->item,
        
'image' => $this->image,
        
'textinput' => $this->textinput,
        
'skiphours' => $this->skiphours,
        
'skipdays' => $this->skipdays
        
        
);
        
        
$this->infos $infos;
        return 
$infos;
    }
    
    
/**
    * @return array
    * @desc Retourne un array comportant les infos de votre choix
    */
    
function return_infos() {
        
$args func_get_args();
        
        
$Ret = array();
        
$T = array('channel''item''image''skipdays''skiphours');
        
        
$infos $this->Parse();
        
        foreach(
$args as $arg) {
            if(
in_array($arg$T))
            
$Ret[$arg] = $infos[$arg];
        }
        
        
$this->infos $Ret;
        return 
$Ret;
    }
    
    
/**
    * @return string HTML
    * @param limit Nombre d item a afficher
    * @desc Affichage Simple des resultats.
    */
    
function Output($limit=10) {
        
$Ret '';
        if(!empty(
$this->infos)) {
            
            
$Ret .= '<div class="tableau">';
            
            
$ret .= '<div class="channel">';
            
$Ret .= '<a href="'.$this->infos['channel']['LINK'].'"';
            
$Ret .= ' title="'.$this->infos['channel']['TITLE'].'"';
            
$Ret .= ' hreflang="'.$this->infos['channel']['LANG'].'">';
            
$Ret .= $this->infos['channel']['TITLE'];
            
$Ret .= '</a>';
            
$Ret .= '</div>';
            
            
$Ret .= '<ul class="item">';
            
            
$i 0;
            foreach(
$this->infos['item'] as $val) {
                
print_r($val);
                
$Ret .= "\t<li><a href=\"$val[LINK]\">$val[TITLE] - ".$val['DC:DATE']."</a></li>\n";
                if(
$i == $limit) break;
                
$i++;
            }
            
            
$Ret .= '</ul>';
            
            
$Ret .= '</div>';
        }
        return 
$Ret;
    }
    
    
// --------------------------------------------------------------------- //
    //                        RSS Creator                                    //
    // --------------------------------------------------------------------- //
    
    
function Creator($file FALSE) {
        
        
$this->channel = array();
        
$this->image = array();
        
$this->textinput = array();
        
$this->skiphours = array();
        
$this->skipdays = array();
        
$this->item = array();
        
        
$this->0;
        
        
$this->File $file;
        
        if (
$this->File && !$this->fp = @fopen($this->File,"w+")) {
            
$this->error_log 'Impossible d ouvrir : '.$this->File;
            die(
$this->error_log);
        }
    }
    
    
/**
    * @return
    * @param encoding string : l encodage de votre fichier XML
    * @desc Ajoute l encodage du fichier
    * @access public
    */
    
function Add_encoding($encoding) {
        
        
$this->encoding $encoding;
    }
    
    
/**
     * @return 
     * @param url URL vers la feuille de style CSS
     * @desc Ajoute une feuille de style au document XML
     * @access public
     */
    
function Add_stylesheet($url) {
        
        
$this->stylesheet_url $url;
    }
    
    
/**
     * @return 
     * @param url URL vers la feuille de style XSLT
     * @desc Ajoute une feuille de style au document XML
     * @access public
     */
    
function Add_XSL_stylesheet($url) {
        
        
$this->XSL_stylesheet_url $url;
    }
    
    
/**
    * @return
    * @param desc string Description du Channel
    * @param lang string langue du Channel
    * @param link string Lien vers le site
    * @param title titre du Channel
    * @desc Ajoute les 4 elements minimum pour que le fichier RSS soit valide
    */
    
function Create_channel($desc$lang$link$title) {
        
        
$this->channel['DESCRIPTION'] = $desc;
        
$this->channel['LANGUAGE'] = $lang;
        
$this->channel['LINK'] = $link;
        
$this->channel['TITLE'] = $title;
    }
    
    function 
Add_copyright($copyright) {
        
        
$this->channel['COPYRIGHT'] = $copyright;
    }
    
    function 
Add_pubdate($pubdate) {
        
        
$this->channel['PUBDATE'] = $pubdate;
    }
    
    function 
Add_lastbuilddate($lastbuilddate) {
        
        
$this->channel['LASTBUILDDATE'] = $lastbuilddate;
    }
    
    function 
Add_docs($docs) {
        
        
$this->channel['DOCS'] = $docs;
    }
    
    function 
Add_rating($rating) {
        
        
$this->channel['RATING'] = $rating;
    }
    
    function 
Add_managingeditor($managingeditor) {
        
        
$this->channel['MANAGINGEDITOR'] = $managingeditor;
    }
    
    function 
Add_webmaster($webmaster) {
        
        
$this->channel['WEBMASTER'] = $webmaster;
    }
    
    function 
Add_image($title$url$link$width=''$height=''$desc='') {
        
        
$this->image['TITLE'] = $title;
        
$this->image['URL'] = $url;
        
$this->image['LINK'] = $link;
        
$this->image['WIDTH'] = $width;
        
$this->image['HEIGHT'] = $height;
        
$this->image['DESCRIPTION'] = $desc;
    }
    
    function 
Add_textinput($title$desc$name$link) {
        
        
$this->textinput['TITLE'] = $title;
        
$this->textinput['DESCRIPTION'] = $desc;
        
$this->textinput['NAME'] = $name;
        
$this->textinput['LINK'] = $link;
    }
    
    function 
Add_skiphours($hours) {
        
        if(
is_array($hours)) {
            foreach(
$hours as $hour) {
                if(
$hour >= || $hour 24)
                
$this->skiphours[] = $hour;
            }
        }
        else
        if(
$hours >= || $hours 24)
        
$this->skiphours[] = $hours;
    }
    
    function 
Add_skipdays($days) {
        
        if(
is_array($days)) {
            foreach(
$days as $day) {
                
$this->skipdays[] = $day;
            }
        }
        else
        
$this->skipdays $days;
    }
    
    function 
Add_item($title$desc$link$addParam = array() ) {
        
        
$this->item[$this->i]['TITLE'] = $title;
        
$this->item[$this->i]['DESCRIPTION'] = $desc;
        
$this->item[$this->i]['LINK'] = $link;
        
        
$addParamList = array (    'AUTHOR',
                                
'CATEGORY_TEXT',
                                
'CATEGORY_DOMAIN',
                                
'COMMENTS',
                                
'GUID_TEXT',
                                
'GUID_ISPERMALINK',
                                
'PUBDATE',
                                
'SOURCE_TEXT',
                                
'SOURCE_URL',
                                
'DCdate',
                                
'DClanguage',
                                
'DCcreator',
                                
'DCsubject',
                                
'CONTENT'
                                
'trackback_ping',
                                
'trackback_about',
                                
'creativeCommons_license',
                                
'enclosure_url',
                                
'enclosure_type',
                                
'enclosure_length' );
        foreach(
$addParamList as $index) {
            isset(
$addParam[$index]) ? $this->item[$this->i][$index] = $addParam[$index] : $this->item[$this->i][$index] = NULL ;
        }
            
        
$this->i++;
    }
    
    function 
Create_file() {
            
        
$R  '<?xml version="1.0" encoding="'.$this->encoding.'" ?>'."\n";
        
        if(
$this->XSL_stylesheet_url)