Logo classes.scriptsphp.org PHP

go to nav bar

simpleRSS

Parsing de fichier RSS grâce a l' extension simplexml.

Cette classe permet de parser et d' afficher des fichiers RSS grâce à l' extension SimpleXML disponible à partir de PHP 5. La classe dispose des mêmes fonctionnailtés que les classes RSS 0.91 et domRSS .

Les Méthodes

Exemples

  1. Spécifications et DTD du format RSS 0.91
  2. Parser un fichier RSS - Affichage Simple
  3. Parser un fichier RSS - Affichage avancé

La Source

<?php


class simplerss {
    
    var 
$delay 600;
    var 
$timeout 6;
    var 
$errorlog '';
    protected 
$O '';
    
// Voir methode Parser()
    
protected $File_str '';
    
    public final function 
__construct() {
        if(!
extension_loaded('simplexml'))
        die(
'Extension SimpleXMl non chargée');
    }
    
    
/*
    ** Methode en "protected" pour permette de la réecrire :
    ** file_get_contents()
    ** simplehttp
    ** simplecurl
    */
    
protected function Parser$file$cache 'NO_CACHE') {
        
        
$url = new simplehttp($file,'GET',$this->delay,$this->timeout);
        if (!
$url->readsocket$cache )) {
            
$this->errorlog .=  "[".__METHOD__ ."] Impossible d ouvrir le fichier : .$file\n";
            return 
FALSE;
        }
        
        
$this->File_str $url->body;
        
        return 
TRUE;
        
    }
    
    public function 
Parse($file) {
        
        if (!
$this->Parser($file)) {
            
$this->errorlog .= "[".__METHOD__ ."] Fichier $file non chargé\n";
            return 
FALSE;
        }
        
$this->File_str preg_replace_callback('`</?[^:"]+?>`', array($this'callback_lower'), $this->File_str);
        if(!(
$this->simplexml_load_string($this->File_str))) {
            
$this->errorlog .= "[".__METHOD__ ."] Fichier $file non parsé\n";
            return 
FALSE;
        }
        return 
$this->O;
    }
    
    private function 
__call($name$arguments) {
        
$this->errorlog .="[$name] Cette methode  n existe pas !!!";
    }
    
    private function 
__get($name) {
        if(
$name === 'Result') {
            
$x $this->O->channel;
            
            if (isset(
$this->O->channel->item)) $it=$this->Get_items($this->O->channel->item);
            if (isset(
$this->O->item)) $it=$this->Get_items($this->O->item);
            
            
$list = array (
            
'title'  ,
            
'description' ,
            
'language' ,
            
'link' ,
            
'pubdate' ,
            
'lastbuilddate' ,
            
'webmaster' ,
            
'managingeditor'
            
);
            foreach(
$list as $val) {
                
$R[$val] = (string) $x->$val;
            }
            
$R['item'] = $it ;
            
            return 
$this->My_map($R);
        }
    }
    
    
    public function 
Output($limit=10) {
        if (!isset(
$this->O->channel)) return FALSE;
        
$x $this->O->channel;
        
        if (isset(
$this->O->channel->item)) $y $this->O->channel->item;
        if (isset(
$this->O->item)) $y $this->O->item;
        
        echo 
'<h1>';
        
$ti htmlentities((string)$x->title);
        
$li htmlentities((string)$x->link);
        
$ln = (string)$x->language;
        echo 
'<a href="',$li,'" title="',$ti,'" hreflang="',$ln,'">';
        echo  
$ti;
        echo 
'</a>';
        echo 
'</h1>'."\n";
        echo 
'<ul class="items">'."\n";
        foreach(
$y as $val) {
            echo 
"\t".'<li>';
            echo 
'<a href="',$val->link,'" title="',utf8_decode($val->title),'" hreflang="',$ln,'">';
            echo 
utf8_decode($val->title);
            echo 
'</a>';
            echo 
'</li>'."\n";
        }
        echo 
'</ul>'."\n";
    }
    
    
    
    private function 
callback_lower($match) {
        return 
strtolower($match[0]);
    }
    
    private function 
callback_map($value)  {
        if(!
is_array($value)) {
            if(
trim($value) === ''$value false;
            return 
utf8_decode($value);
        }
        return 
$value;
    }
    
    private function 
My_map($arr) {
        return 
array_filter(array_map(array($this'callback_map') , $arr));
    }
    
    private function 
Get_items($arr) {
        
$T = array();
        foreach(
$arr as $val) {
            
$T[] = $this->My_map(array('title' =>$val->title 'link' => $val->link'description' => $val->description));
        }
        return 
$T;
    }
}
// end class

?> 

Merci de ne pas suivre ce lien emails.

0.2165s | «»
PHP powered