Advertisement
atmoner

Rss parse

Dec 29th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class ParseurRSS
  5.  
  6. {
  7.  
  8.     // Variables pour l'objet RSS
  9.  
  10.     protected $_urlRSS;
  11.  
  12.     protected $_description;
  13.  
  14.     protected $_language;
  15.  
  16.     protected $_link;
  17.  
  18.     protected $_title;
  19.  
  20.    
  21.  
  22.     //CONSTRUCTEUR
  23.  
  24.     public function __construct(){}
  25.  
  26.  
  27.  
  28.     public function convert2iso($instr){
  29.  
  30.        global $utf8;
  31.  
  32.  
  33.  
  34.        if ($utf8 == 1){
  35.  
  36.           return utf8_decode($instr);
  37.  
  38.        } else {
  39.  
  40.           return $instr;
  41.  
  42.        }
  43.  
  44.     }  
  45.  
  46.     // Methode de Parsage de l'élément
  47.  
  48.     public function parser($urlRSS, $nbElement = null, $tagHtml = null)
  49.  
  50.     {
  51.  
  52.         //Passer lurl rss à l'objet
  53.  
  54.         $this->_urlRSS = $urlRSS;
  55.  
  56.        
  57.  
  58.         //Charger les données du RSS
  59.  
  60.         $rss = simplexml_load_file($this->_urlRSS);
  61.  
  62.  
  63.  
  64.         //Si le Rss est chargé
  65.  
  66.         if($rss)
  67.  
  68.         {
  69.  
  70.             //Vérifier si la langue existe
  71.  
  72.             if(!empty($rss->channel->language))
  73.  
  74.             {
  75.  
  76.                 $this->_language = (string) $rss->channel->language;
  77.  
  78.             }
  79.  
  80.            
  81.  
  82.             //Vérifier si le titre existe
  83.  
  84.             if(!empty($rss->channel->title))
  85.  
  86.             {
  87.  
  88.                 $this->_title = (string) $rss->channel->title; 
  89.  
  90.             }
  91.  
  92.            
  93.  
  94.             //Vérifier si la description existe
  95.  
  96.             if(!empty($rss->channel->description))
  97.  
  98.             {
  99.  
  100.                 $this->_description = (string) $rss->channel->description;     
  101.  
  102.             }
  103.  
  104.            
  105.  
  106.             //Vérifier si le lien existe
  107.  
  108.             if(!empty($rss->channel->link))
  109.  
  110.             {
  111.  
  112.                 $this->_link = (string) $rss->channel->link;       
  113.  
  114.             }
  115.  
  116.  
  117.  
  118.             //Initialisation du compteur
  119.  
  120.             $i = 0;
  121.  
  122.            
  123.  
  124.             //Parcourir les Item du RSS
  125.  
  126.             foreach($rss->channel->item as $item)
  127.  
  128.             {
  129.  
  130.                 //Récupérer les différents infos de l'item
  131.  
  132.                 $itemTitle = (string) $item->title;
  133.  
  134.                 $itemPubDate = (string) $item->pubDate;
  135.  
  136.                 $itemLink = (string) $item->link;
  137.  
  138.                 $itemDescription = (string) $item->description;
  139.  
  140.                
  141.  
  142.                 // Si on veut pas des tags html.
  143.  
  144.                 if($tagHtml == false)
  145.  
  146.                 {
  147.  
  148.                     //On les vire
  149.  
  150.                     $itemDescription = strip_tags($itemDescription);
  151.  
  152.                 }
  153.  
  154.                
  155.  
  156.                 //Compteur pour les catégories
  157.  
  158.                 $y = 0;
  159.  
  160.                
  161.  
  162.                 //Pour les catégories de chaque Item
  163.  
  164.                 foreach($item->category as $cat)
  165.  
  166.                 {
  167.  
  168.                     // Stocker dans un tableau
  169.  
  170.                     $itemCategory[$y] = (string) $cat;
  171.  
  172.                    
  173.  
  174.                     // Incrémenter
  175.  
  176.                     $y++;
  177.  
  178.                 }
  179.  
  180.                  
  181.  
  182.                 //Remplir le tableau associatif contenant les élements ciblés du RSS
  183.  
  184.                 $retourParser[$i] = array('title'=>$itemTitle,'pubDate'=>$itemPubDate,'description'=>$itemDescription,'link'=>$itemLink,'category'=>$itemCategory);
  185.  
  186.                
  187.  
  188.                 //Incrémenter
  189.  
  190.                 $i++;
  191.  
  192.            
  193.  
  194.                 //Si le nombre de cellule est égale au nombre d'Item voulu
  195.  
  196.                 if(count($retourParser) == $nbElement)
  197.  
  198.                 {
  199.  
  200.                     //On arrête la boucle
  201.  
  202.                     break;
  203.  
  204.                 }
  205.  
  206.             }
  207.  
  208.             //Retourner le tableau
  209.  
  210.             return $retourParser;
  211.  
  212.         }
  213.  
  214.         else
  215.  
  216.         {
  217.  
  218.             return "Impossible de charger le flux RSS.";
  219.  
  220.         }
  221.  
  222.     }
  223.  
  224.    
  225.  
  226.     public function getTitle()
  227.  
  228.     {
  229.  
  230.         return $this->_title;
  231.  
  232.     }
  233.  
  234.    
  235.  
  236.     public function getLink()
  237.  
  238.     {
  239.  
  240.         return $this->_link;
  241.  
  242.     }
  243.  
  244.    
  245.  
  246.     public function getDescription()
  247.  
  248.     {
  249.  
  250.         return $this->_description;
  251.  
  252.     }
  253.  
  254.    
  255.  
  256.     public function getLanguage()
  257.  
  258.     {
  259.  
  260.         return $this->_language;
  261.  
  262.     }
  263.  
  264.    
  265.  
  266.     public function getUrlRss()
  267.  
  268.     {
  269.  
  270.         return $this->_urlRSS;
  271.  
  272.     }
  273.  
  274. }
  275.  
  276. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement