voici un premier jet de ma classe(elle ne respecte pas le schema données dans le poste audessus), j'ai plusieur methode qui se ressemble et j'aimerai bien simplifié mais la , j'y vois plus tres clair
<?php
class Rss2
{
private $file;
public $lang;
private $item;
private $fp;
private $encoding;
private $channel;
public $xslt;
private $image_item;
public $copyright;
public $lastBuildDate;
public function __construct($file)
{
$this->channel=array();
$this->encoding='UTF-8';
$this->lang='en';
$this->file=$file;
$this->iten=array();
}
public function __set($name,$value)
{
$this->$name=$value;
}
public function Create()
{
try{
if(!($this->fp=fopen($this->file,"w")))
throw new MyException('impossible d\'ouvrir :'.$this->file);
}
catch (MyException $e)
{
$e->exceptionlog();
}
}
/**
* ajoute des items
*
* @param string $titre
* @param string $description
* @param url $lien
*/
public function add_item($titre,$description,$lien)
{
try
{
if(isset($titre) && isset($description) && isset($lien))
{
$this->item[]=array('title'=>$titre,'description'=>$description,'link'=>$lien);
}
else
throw new MyException("les arguments 'titre','description','lien' sont obligatoires");
}
catch (MyException $e)
{$e->exceptionlog();}
}
/**
* information sur le channel
*
* @param string $titre
* @param string $description
* @param url $lien
* @param date $date
*/
public function infoChannel($titre,$description,$lien,$date)
{
try
{
if(isset($titre) && isset($description) && isset($lien))
$this->channel=array('title'=>$titre,'description'=>$description,'link'=>$lien);
else
throw new MyException("les arguments 'titre','description','lien' sont obligatoires");
$this->channel['date']=$date;
}
catch (MyException $e)
{$e->exceptionlog();}
}
public function OptionItemImage($lien,$urlimage,$titre,$description,$width,$height)
{
try {
if(isset($titre) && isset($description) && isset($urlimage))
$this->image_item[]=array('url'=>$urlimage,'title'=>$titre,'description'=>$description,'link'=>$lien,'width'=>$width,'height'=>$height);
else
throw new MyException("les arguments 'titre','description','lien' sont obligatoires");
}
catch (MyException $e)
{$e->exceptionlog();}
}
/**
* ecrit un fichier rss en xml
*
* @return boolean
*/
public function WriteRss()
{
//echo '<pre>'.print_r($this->item).'</pre>';
$rss='<?xml version="1.0" encoding="'.$this->encoding.'" ?>'."\n";
if(!empty($this->xslt))
$rss.='<?xml-stylesheet href="'.$this->xslt.'" type="text/xsl"?>'."\n";
$rss.='<rss version="2.0">'."\n";
$rss.='<channel>'."\n";
$rss.='<title>'.$this->channel['title'].'</title>'."\n";
$rss.='<link>'.$this->channel['link'].'</link>'."\n";
$rss.='<description>'.$this->channel['description'].'</description>'."\n";
$rss.='<language>'.$this->lang.'</language>'."\n";
if(!empty($this->copyright))
$rss.='<copyright>'.$this->copyright.'</copyright>';
if(!empty($this->lastBuildDate))
$rss.='<lastBuildDate>'.$this->lastBuildDate.'<lastBuildDate>';
count($this->item)==count($this->image_item)?$flag=1:$flag=0;
foreach ($this->item as $item=>$val)
{
$rss.='<item>'."\n";
$rss.='<title>'.$val['title'].'</title>'."\n";
$rss.='<link>'.$val['link'].'</link>'."\n";
$rss.='<description>'.$val['description'].'</description>'."\n";
if(!empty($this->image_item)&& $flag==1)
{
$rss.='<image>';
$rss.='<url>'.$this->image_item['urlimage'].'</url>';
$rss.='<title>'.$this->image_item['title'].'</title>';
$rss.='<link>'.$this->image_item['link'].'</link>';
$rss.='<width>'.$this->image_item['width'].'</width>';
$rss.='<height>'.$this->image_item['height'].'</height>';
$rss.='</image>';
}
$rss.='</item>'."\n";
}
$rss.='</channel>';
$rss.='</rss>'."\n";
if ($this->file) {
fwrite($this->fp, $rss);
fclose($this->fp);
return true;
}
else
return false;
}
}
?>
et l'utilisation
include 'config/parametre.ini.php';
function __autoload($nom)
{
require dirname(__file__).'/lib/class/'.$nom.'.php';
}
$mysql=new Mysqldb(SERVEUR,LOGIN,PASS,BASE);
$dao=new DAO($mysql);
$rss=new Rss2('RSS/actu.xml');
$rss->Create();
$rss->lang='fr';
$rss->infoChannel('essai','premier test','http://localhost/projet_ing/',date("d/m/Y-H:i:s"));
$champs=array('date','description','titre','id_a');
$table=array('actu');
$dao->SetVar($table,$champs);
foreach($dao->Select('','Limit 0,4') as $cle=>$tab)
{
$rss->add_item($tab[2],$tab[1],'http://localhost/projet_ing/index.php?module=actu&action=display&id='.$tab[3]);
}
$rss->WriteRss();
?>
pour ceux que cela interresse un lien sur le rss et xml en general
l
http://xmlfr.org/documentations/tutoriels/041022-0001