Si tu veux a l'époque j'avais écris cette atroce outil pour manipuler les flux RSS:
Code : Tout sélectionner
<?php
class RSSFactory{
//Pour créer une passerelle avec un RSS Externe
public static function getExternRSS($chemin){
return new RSS($chemin, false);
}
//Pour convertir un fichier PHP en flux RSS composé grâce aux outils plus bas
public static function makeRSS(){
return new RSS(false, true);
}
//Créer un fichier RSS physique
public static function buildRSS($chemin){
return new RSS($chemin, true);
}
//Crée un item vide à remplir
public static function buildClearItem(){
return new RSSItem();
}
//Créer un item avec le stricte minimum
public static function buildStrictItem($titre, $lien, $description, $auteur, $date){
$item = new RSSItem();
$item->setAuteur($auteur);
$item->setTitre($titre);
$item->setLien($lien);
$item->setDescription($description);
$item->setDate($date);
return $item;
}
//Crée un item complet
public static function buildItem($titre, $lien, $description, $auteur, $date, $categorie, $commentaire, $guid){
$item = new RSSItem();
$item->setAuteur($auteur);
$item->setTitre($titre);
$item->setLien($lien);
$item->setDescription($description);
$item->setDate($date);
$item->setCategorie($categorie);
$item->setCommentaire($commentaire);
$item->setGuid($guid);
return $item;
}
}
class RSS{
private $chemin;
private $ecriture;
private $rss;
private $channel;
public static function is_picture($url){
if(!is_file($url))return false;
$url = substr($url,strrpos($url,".")+1);
$url = strtolower($url);
$extension = array('gif','jpg','png','jpeg');
return in_array($url, $extension);
}
public function __construct($chemin, $ecriture){
$this->chemin = $chemin;
$this->ecriture = $ecriture;
if($this->ecriture){
$this->rss = new SimpleXMLElement('<rss/>');
$this->rss->addAttribute('version', '2.0');
$this->channel = $this->rss->addChild('channel');
}else{
$curlConnexion = curl_init();
curl_setopt($curlConnexion, CURLOPT_URL, $this->chemin);
curl_setopt($curlConnexion, CURLOPT_POST, 1);
curl_setopt($curlConnexion, CURLOPT_RETURNTRANSFER, 1);
$xmlExternString = curl_exec($curlConnexion);
$this->rss = simplexml_load_string($xmlExternString);
}
}
//Gestion du Channel du flux RSS
//Uniquement possible en mode écriture
public function setChannelTitre($title){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('title', $title);
}
public function setChannelLien($link){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('link', $link);
}
public function setChannelDescription($desc){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('description', $desc);
}
public function setChannelLangage($lang){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('language', $lang);
}
public function setChannelImage($url){
if(!$this->ecriture)throw new Exception("Cannot write file");
if(!self::is_picture($url))throw new Exception("Not a picture");
$this->channel->addChild('image', $url);
}
public function setChannelCopyright($copy){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('copyright', $copy);
}
public function setChannelManaging($manage){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('managingEditor', $manage);
}
public function setChannelWebmaster($webmaster){
if(!$this->ecriture)throw new Exception("Cannot write file");
$this->channel->addChild('webmaster', $webmaster);
}
public function addItem(RSSItem $item){
if($item->getTitre() === null || $item->getLien() === null || $item->getDescription() === null)
throw new Exception("Vital Datas are empty");
$newItem = $this->channel->addChild('item');
$newItem->addChild('title', $item->getTitre());
$newItem->addChild('link', $item->getLien());
$newItem->addChild('description', $item->getDescription());
if($item->getAuteur() !== null)$newItem->addChild('auteur', $item->getAuteur());
if($item->getCategorie() !== null)$newItem->addChild('category', $item->getCategorie());
if($item->getCommentaire() !== null)$newItem->addChild('comments', $item->getCommentaire());
if($item->getGuid() !== null)$newItem->addChild('guid', $item->getGuid());
if($item->getDate() !== null)$newItem->addChild('pubDate', $item->getDate());
}
//Gestion du flux externe
public function getChannelInfos(){
$array = array(
"titre" => $this->rss->channel->title,
"lien" => $this->rss->channel->link,
"description" => $this->rss->channel->description,
"langue" => $this->rss->channel->language,
"image" => $this->rss->channel->image,
);
return $array;
}
public function fetch($nb = -1){
$iteration = 0;
foreach($this->rss->channel->item as $item){
$listItem[] = array(
"titre"=>(string)($item->title),
"lien"=>(string)($item->link),
"description"=>(string)($item->description)
);
if($nb != -1)if((++$iteration>=$nb))break;
}
return $listItem;
}
//Fin et affichage du fichier
public function finalise(){
if(!$this->ecriture || !$this->chemin){
header('content-type: application/xml');
echo $this->rss->asXML();
}
else $this->rss->asXML($this->chemin);
}
}
class RSSItem{
private $titre;
private $lien;
private $description;
private $auteur;
private $categorie;
private $commentaire;
private $guid;
private $date;
public function __construct(){
$this->titre = null;
$this->lien = null;
$this->description = null;
$this->auteur = null;
$this->categorie = null;
$this->commentaire = null;
$this->guid = null;
$this->date = null;
}
public function getTitre(){ return $this->titre; }
public function getLien(){ return $this->lien; }
public function getDescription(){ return $this->description; }
public function getAuteur(){ return $this->auteur; }
public function getCategorie(){ return $this->categorie; }
public function getCommentaire(){ return $this->commentaire; }
public function getGuid(){ return $this->guid; }
public function getDate(){ return $this->date; }
public function setTitre($var){
$this->titre = $var;
}
public function setLien($var){
$this->lien = $var;
}
public function setDescription($var){
$this->description = $var;
}
public function setAuteur($var){
$this->auteur = $var;
}
public function setCategorie($var){
$this->categorie = $var;
}
public function setCommentaire($var){
$this->commentaire = $var;
}
public function setGuid($var){
$this->guid = $var;
}
public function setDate($var){
$this->date = $var;
}
}
?>