Accueil > > > [PHP5]GENERATION DE FLUX RSS
[PHP5]GENERATION DE FLUX RSS
Information sur la source
Description
Petite classe permettant la génération de flux RSS statique (feed.rss) ou dynamique (fichier php générant le flux mais qui execute une requête, donc imaginer le nombre de requête si tous vos membres veulent accéder à votre flux...) je ne connais pas bien le langage XML et la POO, c'est récent pour moi, merci de m'aiguiller si j'ai fait des erreurs ( c'est même sûr !) Oublier pas d'indiquer l'adresse de votre flux : <a type="application/rss+xml" href="feed.xml> Flux RSS de cette page</a>
Source
- <?php
- /**
- * RSS200 Object.
- *
- * @name RSS200
- * @since 1.0
- *
- * @author Berlemont Quentin
- * @copyright 2006 Berlemont Quentin
- *
- */
- class RSS200 {
-
- /**
- * @desc RSS property
- *
- * @access private
- * @since 1.0
- * @type array
- */
- private $RSSarr = array('encoding' => '',
- 'stylesheet' => '',
- 'version' => '',
- 'title' => '',
- 'link' => '',
- 'description' => '',
- 'language' => '',
- 'copyright' => '',
- 'managingEditor' => '',
- 'webMaster' => '',
- 'pubDate' => '',
- 'lastBuildDate' => '',
- 'category' => array ('category' => '',
- 'domain' => ''),
- 'generator' => 'RSS generated by Quentin Berlemont\'s RSS200 Class',
- 'docs' => 'http://blogs.law.harvard.edu/tech/rss',
- 'cloud' => array('domain' => '',
- 'port' => '',
- 'path' => '',
- 'registerProcedure' => '',
- 'protocol' => ''),
- 'ttl' => '',
- 'image' => array('url' => '',
- 'title' => '',
- 'link' => '',
- 'width' => '88',
- 'height' => '31'),
- 'rating' => '',
- 'textInput' => array('title' => '',
- 'description' => '',
- 'name' => '',
- 'link' => ''),
- 'skipHours' => array(0 => 0,
- 1 => 1,
- 2 => 2,
- 3 => 3,
- 4 => 4,
- 5 => 5,
- 6 => 6,
- 7 => 7,
- 8 => 8,
- 9 => 9,
- 10 => 10,
- 11 => 11,
- 12 => 12,
- 13 => 13,
- 14 => 14,
- 15 => 15,
- 16 => 16,
- 17 => 17,
- 18 => 18,
- 19 => 19,
- 20 => 20,
- 21 => 21,
- 22 => 22,
- 23 => 23),
- 'skipDays' => array('Monday' => 'Monday',
- 'Tuesday' => 'Tuesday',
- 'Wednesday' => 'Wednesday',
- 'Thursday' => 'Thursday',
- 'Friday' => 'Friday',
- 'Saturday' => 'Saturday',
- 'Sunday' => 'Sunday'),
- 'item' => array('title' => '',
- 'link' => '',
- 'description' => '',
- 'author' => '',
- 'category' => array ('category' => '',
- 'domain' => ''),
- 'comments' => '',
- 'enclosure' => array('url' => '',
- 'length' => '',
- 'type' => ''),
- 'guid' => array('guid' => '',
- 'isPermaLink' => ''),
- 'pubDate' => '',
- 'source' => array('source' => '',
- 'url' => '')
- )
- );
-
- /**
- * @desc RSS Buffer
- *
- * @access private
- * @since 1.0
- * @type string
- */
- private $RSSbuffer;
-
- /**
- * @desc RSS Save Path
- *
- * @access private
- * @since 1.0
- * @type string
- */
- private $RSSpath;
-
- /**
- * @desc RSS Filename
- *
- * @access private
- * @since 1.0
- * @type string
- */
- private $RSSname;
-
- /**
- * @desc Create a new RSS200 Object instance.
- *
- * @param string A RssPath
- * @param string A RssName
- *
- * @access public
- * @since 1.0
- */
- public function __construct($RssPath, $RssName) {
- if(is_dir($RssPath))
- $this -> RSSpath = $RssPath;
- $this -> RSSname = $RssName . '.xml';
- }
-
- /**
- * @desc Open a RSS Feed.
- *
- * @param array A ArgumentsArr
- *
- * @access public
- * @since 1.0
- */
- public function OpenFeed($ArgumentsArr = array ()) {
- foreach ($this -> RSSarr as $key => $value) {
- if (array_key_exists ($key, $ArgumentsArr)) {
- $this -> RSSarr[$key] = $ArgumentsArr[$key];
- }
- else if (!empty ($value)) {
- $this -> RSSarr[$key] = $value;
- }
- }
- $this -> RSSbuffer = '<?xml version="1.0" encoding="' . $this -> RSSarr['encoding'] . '" ?>';
-
- if (!empty($this -> RSSarr['stylesheet']))
- $this -> RSSbuffer .= '<?xml-stylesheet type="text/xsl" title="RSS" href="' . $this -> RSSarr['stylesheet'] . '"?>';
-
- $this -> RSSbuffer .= '<rss version="' . $this -> RSSarr['version'] . '">';
- $this -> RSSbuffer .= ' <channel>';
- $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['title'] . '</title>';
- $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['link'] . '</link>';
- $this -> RSSbuffer .= ' <description>' . $this -> RSSarr['description'] . '</description>';
-
- if (!empty($this -> RSSarr['language']))
- $this -> RSSbuffer .= ' <language>' . $this -> RSSarr['language'] . '</language>';
-
- if (!empty($this -> RSSarr['copyright']))
- $this -> RSSbuffer .= ' <copyright>' . $this -> RSSarr['copyright'] . '</copyright>';
-
- if (!empty($this -> RSSarr['managingEditor']))
- $this -> RSSbuffer .= ' <managingEditor>' . $this -> RSSarr['managingEditor'] . '</managingEditor>';
-
- if (!empty($this -> RSSarr['webMaster']))
- $this -> RSSbuffer .= ' <webMaster>' . $this -> RSSarr['webMaster'] . '</webMaster>';
-
- if (!empty($this -> RSSarr['pubDate']))
- $this -> RSSbuffer .= ' <pubDate>' . $this -> RSSarr['pubDate'] . '</pubDate>';
-
- if (!empty($this -> RSSarr['lastBuildDate']))
- $this -> RSSbuffer .= ' <lastBuildDate>' . $this -> RSSarr['lastBuildDate'] . '</lastBuildDate>';
-
- if (!empty($this -> RSSarr['category']))
- $this -> RSSbuffer .= ' <category domain="' . $this -> RSSarr['category']['domain'] . '">' . $this -> RSSarr['category']['category'] . '</category>';
-
- if (!empty($this -> RSSarr['generator']))
- $this -> RSSbuffer .= ' <generator>' . $this -> RSSarr['generator'] . '</generator>';
-
- if (!empty($this -> RSSarr['doc']))
- $this -> RSSbuffer .= ' <doc>' . $this -> RSSarr['doc'] . '</doc>';
-
- if (!empty($this -> RSSarr['cloud']))
- $this -> RSSbuffer .= ' <cloud domain="' . $this -> RSSarr['cloud']['domain'] . '" port="' . $this -> RSSarr['cloud']['port'] . '" path="' . $this -> RSSarr['cloud']['path'] . '" registerProcedure="' . $this -> RSSarr['cloud']['registerProcedure'] . '" protocol="' . $this -> RSSarr['cloud']['protocol'] . '"/>';
-
- if (!empty($this -> RSSarr['ttl']))
- $this -> RSSbuffer .= ' <ttl>' . $this -> RSSarr['ttl'] . '</ttl>';
-
- if (!empty($this -> RSSarr['image'])) {
- $this -> RSSbuffer .= ' <image>';
-
- if (!empty($this -> RSSarr['image']['url']))
- $this -> RSSbuffer .= ' <url>' . $this -> RSSarr['image']['url'] . '</url>';
-
- if (!empty($this -> RSSarr['image']['title']))
- $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['image']['title'] . '</title>';
-
- if (!empty($this -> RSSarr['image']['link']))
- $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['image']['link'] . '</link>';
-
- if (!empty($this -> RSSarr['image']['width']))
- $this -> RSSbuffer .= ' <width>' . $this -> RSSarr['image']['width'] . '</width>';
-
- if (!empty($this -> RSSarr['image']['height']))
- $this -> RSSbuffer .= ' <height>' . $this -> RSSarr['image']['height'] . '</height>';
-
- $this -> RSSbuffer .= ' </image>';
- }
-
- //if (!empty($this -> RSSarr['rating'])) Comment marche cette proriete ?
- //$this -> RSSbuffer .= ' <rating>' . $this -> RSSarr['rating'] . '</rating>';
-
- if (!empty($this -> RSSarr['textInput'])) {
- $this -> RSSbuffer .= ' <textInput>';
-
- if (!empty($this -> RSSarr['textInput']['title']))
- $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['textInput']['title'] . '</title>';
-
- if (!empty($this -> RSSarr['textInput']['description']))
- $this -> RSSbuffer .= ' <description>' . $this -> RSSarr['textInput']['description'] . '</description>';
-
- if (!empty($this -> RSSarr['textInput']['name']))
- $this -> RSSbuffer .= ' <name>' . $this -> RSSarr['textInput']['name'] . '</name>';
-
- if (!empty($this -> RSSarr['textInput']['link']))
- $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['textInput']['link'] . '</link>';
-
- $this -> RSSbuffer .= ' </textInput>';
- }
-
- if (!empty($this -> RSSarr['skipHours']))
- $this -> RSSbuffer .= ' <hour>' . $this -> RSSarr['skipHours'] . '</hour>';
-
- if (!empty($this -> RSSarr['skipDays']))
- $this -> RSSbuffer .= ' <day>' . $this -> RSSarr['skipDays'] . '</day>';
- }
-
- /**
- * @desc Add item in the RSS Feed.
- *
- * @param array A element
- * @param array A ArgumentsArr
- *
- * @access public
- * @since 1.0
- */
- public function AddItem($element, $ArgumentsArr) {
- if (!array_key_exists ($element, $this -> RSSarr))
- throw new Exception ($element . ' n\'est pas un élément RSS valide');
-
- foreach ($this -> RSSarr[$element] as $key => $value) {
- if (array_key_exists ($key, $ArgumentsArr))
- $this -> RSSarr[$element][$key] = $ArgumentsArr[$key];
- else if (!empty ($value))
- $this -> RSSarr[$element][$key] = $value;
- }
-
- if (!empty($this -> RSSarr['item'])) {
- $this -> RSSbuffer .= ' <item>';
-
- if (!empty($this -> RSSarr['item']['title']))
- $this -> RSSbuffer .= ' <title>' . $this -> RSSarr['item']['title'] . '</title>';
-
- if (!empty($this -> RSSarr['item']['link']))
- $this -> RSSbuffer .= ' <link>' . $this -> RSSarr['item']['link'] . '</link>';
-
- if (!empty($this -> RSSarr['item']['description']))
- $this -> RSSbuffer .= ' <description>' . $this -> RSSarr['item']['description'] . '</description>';
-
- if (!empty($this -> RSSarr['item']['author']))
- $this -> RSSbuffer .= ' <author>' . $this -> RSSarr['item']['author'] . '</author>';
-
- if (!empty($this -> RSSarr['item']['category']))
- $this -> RSSbuffer .= ' <category domain="' . $this -> RSSarr['item']['category']['domain'] . '">' . $this -> RSSarr['item']['category']['category'] . '</category>';
-
- if (!empty($this -> RSSarr['item']['comment']))
- $this -> RSSbuffer .= ' <comments>' . $this -> RSSarr['item']['comments'] . '</comments>';
-
- if (!empty($this -> RSSarr['item']['enclosure']))
- $this -> RSSbuffer .= ' <enclosure url="' . $this -> RSSarr['item']['enclosure']['url'] . '" length="' . $this -> RSSarr['item']['enclosure']['length'] . '" type="' . $this -> RSSarr['item']['enclosure']['type'] . '" />';
-
- if (!empty($this -> RSSarr['item']['guid']))
- $this -> RSSbuffer .= ' <guid isPermaLink="' . $this -> RSSarr['item']['guid']['isPermaLink'] . '">' . $this -> RSSarr['item']['guid']['guid'] . '</guid>';
-
- if (!empty($this -> RSSarr['item']['pubDate']))
- $this -> RSSbuffer .= ' <pubDate>' . $this -> RSSarr['item']['pubDate'] . '</pubDate>';
-
- if (!empty($this -> RSSarr['item']['source']))
- $this -> RSSbuffer .= ' <source url="' . $this -> RSSarr['item']['source']['url'] . '">' . $this -> RSSarr['item']['source']['source'] . '</source>';
-
- $this -> RSSbuffer .= ' </item>';
- }
- }
-
- /**
- * @desc Close RSS Feed.
- *
- * @access public
- * @since 1.0
- */
- public function CloseFeed() {
- $this -> RSSbuffer .= '</channel>';
- $this -> RSSbuffer .= '</rss>';
- }
-
- /**
- * @desc Save RSS Feed in a XML file.
- *
- * @access public
- * @since 1.0
- */
- public function SaveFeed() {
- if (@is_file($this -> RSSpath . $this -> RSSname))
- @unlink($this -> RSSpath . $this -> RSSname);
- $fopen = @fopen($this -> RSSpath . $this -> RSSname, 'w+');
- if (!$fopen)
- throw new Exception('Impossible d\'ouvrir un fichier' . $this -> RSSpath . $this -> RSSname);
- fwrite($fopen, $this -> RSSbuffer);
- fclose($fopen);
- }
-
- /**
- * @desc Show RSS Feed.
- *
- * @return string RSSbuffer content the RSS feed
- *
- * @access public
- * @since 1.0
- */
- public function __toString() {
- header('Content-type: text/xml');
- return $this -> RSSbuffer;
- }
-
- /**
- * @desc destruct RSS200 Object instance.
- *
- * @access public
- * @since 1.0
- */
- public function __destruct () {
- unset ($this);
- }
- }
- ?>
-
- EXEMPLE D UTILISATION
-
- <?php
- include_once('RSS200.class.php');
-
- // try to create RSS Feed.
- try {
- $FeedPath = ''; // Directory where the XML file save, WITH '/' at the end.
- $FeeName = ''; // Name of the RSS Feer, for exemle 'news' NOT 'new.xml' !
-
- // new RSS200 Object instance.
- $rss = new RSS200($FeedPath, $FeeName);
-
- // Open RSS Feed.
- $rss -> OpenFeed(array( 'encoding' => 'utf-8',
- 'stylesheet' => '',
- 'version' => '2.0',
- 'title' => 'MonSite',
- 'link' => 'http://www.MonSite.com',
- 'description' => 'Mon Super site !',
- 'language' => 'fr-FR',
- 'copyright' => 'Copyright by Moi',
- 'managingEditor' => 'Moi',
- 'webMaster' => 'Moi',
- 'pubDate' => '',
- 'lastBuildDate' => '',
- 'category' => array ('category' => '',
- 'domain' => ''),
- 'generator' => 'RSS generated by Quentin Berlemont\'s RSS200 Class', // LESS IT, THANX.
- 'docs' => 'http://blogs.law.harvard.edu/tech/rss', // FULL DOCUMENTATION ABOUT XML (en) AND http://www.stervinou.com/projets/rss/ (fr).
- 'cloud' => array('domain' => '',
- 'port' => '',
- 'path' => '',
- 'registerProcedure' => '',
- 'protocol' => ''),
- 'ttl' => '',
- 'image' => array('url' => '',
- 'title' => '',
- 'link' => '',
- 'width' => '88',
- 'height' => '31'),
- 'rating' => '',
- 'textInput' => array('title' => '',
- 'description' => '',
- 'name' => '',
- 'link' => ''),
- 'skipHours' => '', // Number between 0 and 23.
- 'skipDays' => '', // Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday.
- )
- );
- $rss -> AddItem('item', array('title' => 'test 1',
- 'link' => '',
- 'description' => ' description 1',
- 'author' => '',
- 'category' => array ('category' => '',
- 'domain' => ''),
- 'comments' => '',
- 'enclosure' => array('url' => '',
- 'length' => '',
- 'type' => ''),
- 'guid' => array('guid' => '',
- 'isPermaLink' => ''),
- 'pubDate' => '',
- 'source' => array('source' => '',
- 'url' => '')
- )
- );
- // Close RSS Feed, DON't FORGET IT !
- $rss -> CloseFeed();
- echo $rss; // Show RSS Feed.
- //$rss -> SaveFeed(); // Save RSS Feed in a XML file.
- }
- //if Error, show it
- catch(Exception $e) {
- echo '<pre>', print_r($e), '</pre>';
- }
- ?>
<?php
/**
* RSS200 Object.
*
* @name RSS200
* @since 1.0
*
* @author Berlemont Quentin
* @copyright 2006 Berlemont Quentin
*
*/
class RSS200 {
/**
* @desc RSS property
*
* @access private
* @since 1.0
* @type array
*/
private $RSSarr = array('encoding' => '',
'stylesheet' => '',
'version' => '',
'title' => '',
'link' => '',
'description' => '',
'language' => '',
'copyright' => '',
'managingEditor' => '',
'webMaster' => '',
'pubDate' => '',
'lastBuildDate' => '',
'category' => array ('category' => '',
'domain' => ''),
'generator' => 'RSS generated by Quentin Berlemont\'s RSS200 Class',
'docs' => 'http://blogs.law.harvard.edu/tech/rss',
'cloud' => array('domain' => '',
'port' => '',
'path' => '',
'registerProcedure' => '',
'protocol' => ''),
'ttl' => '',
'image' => array('url' => '',
'title' => '',
'link' => '',
'width' => '88',
'height' => '31'),
'rating' => '',
'textInput' => array('title' => '',
'description' => '',
'name' => '',
'link' => ''),
'skipHours' => array(0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
11 => 11,
12 => 12,
13 => 13,
14 => 14,
15 => 15,
16 => 16,
17 => 17,
18 => 18,
19 => 19,
20 => 20,
21 => 21,
22 => 22,
23 => 23),
'skipDays' => array('Monday' => 'Monday',
'Tuesday' => 'Tuesday',
'Wednesday' => 'Wednesday',
'Thursday' => 'Thursday',
'Friday' => 'Friday',
'Saturday' => 'Saturday',
'Sunday' => 'Sunday'),
'item' => array('title' => '',
'link' => '',
'description' => '',
'author' => '',
'category' => array ('category' => '',
'domain' => ''),
'comments' => '',
'enclosure' => array('url' => '',
'length' => '',
'type' => ''),
'guid' => array('guid' => '',
'isPermaLink' => ''),
'pubDate' => '',
'source' => array('source' => '',
'url' => '')
)
);
/**
* @desc RSS Buffer
*
* @access private
* @since 1.0
* @type string
*/
private $RSSbuffer;
/**
* @desc RSS Save Path
*
* @access private
* @since 1.0
* @type string
*/
private $RSSpath;
/**
* @desc RSS Filename
*
* @access private
* @since 1.0
* @type string
*/
private $RSSname;
/**
* @desc Create a new RSS200 Object instance.
*
* @param string A RssPath
* @param string A RssName
*
* @access public
* @since 1.0
*/
public function __construct($RssPath, $RssName) {
if(is_dir($RssPath))
$this -> RSSpath = $RssPath;
$this -> RSSname = $RssName . '.xml';
}
/**
* @desc Open a RSS Feed.
*
* @param array A ArgumentsArr
*
* @access public
* @since 1.0
*/
public function OpenFeed($ArgumentsArr = array ()) {
foreach ($this -> RSSarr as $key => $value) {
if (array_key_exists ($key, $ArgumentsArr)) {
$this -> RSSarr[$key] = $ArgumentsArr[$key];
}
else if (!empty ($value)) {
$this -> RSSarr[$key] = $value;
}
}
$this -> RSSbuffer = '<?xml version="1.0" encoding="' . $this -> RSSarr['encoding'] . '" ?>';
if (!empty($this -> RSSarr['stylesheet']))
$this -> RSSbuffer .= '<?xml-stylesheet type="text/xsl" title="RSS" href="' . $this -> RSSarr['stylesheet'] . '"?>';
$this -> RSSbuffer .= '<rss version="' . $this -> RSSarr['version'] . '">';
$this -> RSSbuffer .= ' <channel>';
$this -> RSSbuffer .= ' <title>' . $this -> RSSarr['title'] . '</title>';
$this -> RSSbuffer .= ' <link>' . $this -> RSSarr['link'] . '</link>';
$this -> RSSbuffer .= ' <description>' . $this -> RSSarr['description'] . '</description>';
if (!empty($this -> RSSarr['language']))
$this -> RSSbuffer .= ' <language>' . $this -> RSSarr['language'] . '</language>';
if (!empty($this -> RSSarr['copyright']))
$this -> RSSbuffer .= ' <copyright>' . $this -> RSSarr['copyright'] . '</copyright>';
if (!empty($this -> RSSarr['managingEditor']))
$this -> RSSbuffer .= ' <managingEditor>' . $this -> RSSarr['managingEditor'] . '</managingEditor>';
if (!empty($this -> RSSarr['webMaster']))
$this -> RSSbuffer .= ' <webMaster>' . $this -> RSSarr['webMaster'] . '</webMaster>';
if (!empty($this -> RSSarr['pubDate']))
$this -> RSSbuffer .= ' <pubDate>' . $this -> RSSarr['pubDate'] . '</pubDate>';
if (!empty($this -> RSSarr['lastBuildDate']))
$this -> RSSbuffer .= ' <lastBuildDate>' . $this -> RSSarr['lastBuildDate'] . '</lastBuildDate>';
if (!empty($this -> RSSarr['category']))
$this -> RSSbuffer .= ' <category domain="' . $this -> RSSarr['category']['domain'] . '">' . $this -> RSSarr['category']['category'] . '</category>';
if (!empty($this -> RSSarr['generator']))
$this -> RSSbuffer .= ' <generator>' . $this -> RSSarr['generator'] . '</generator>';
if (!empty($this -> RSSarr['doc']))
$this -> RSSbuffer .= ' <doc>' . $this -> RSSarr['doc'] . '</doc>';
if (!empty($this -> RSSarr['cloud']))
$this -> RSSbuffer .= ' <cloud domain="' . $this -> RSSarr['cloud']['domain'] . '" port="' . $this -> RSSarr['cloud']['port'] . '" path="' . $this -> RSSarr['cloud']['path'] . '" registerProcedure="' . $this -> RSSarr['cloud']['registerProcedure'] . '" protocol="' . $this -> RSSarr['cloud']['protocol'] . '"/>';
if (!empty($this -> RSSarr['ttl']))
$this -> RSSbuffer .= ' <ttl>' . $this -> RSSarr['ttl'] . '</ttl>';
if (!empty($this -> RSSarr['image'])) {
$this -> RSSbuffer .= ' <image>';
if (!empty($this -> RSSarr['image']['url']))
$this -> RSSbuffer .= ' <url>' . $this -> RSSarr['image']['url'] . '</url>';
if (!empty($this -> RSSarr['image']['title']))
$this -> RSSbuffer .= ' <title>' . $this -> RSSarr['image']['title'] . '</title>';
if (!empty($this -> RSSarr['image']['link']))
$this -> RSSbuffer .= ' <link>' . $this -> RSSarr['image']['link'] . '</link>';
if (!empty($this -> RSSarr['image']['width']))
$this -> RSSbuffer .= ' <width>' . $this -> RSSarr['image']['width'] . '</width>';
if (!empty($this -> RSSarr['image']['height']))
$this -> RSSbuffer .= ' <height>' . $this -> RSSarr['image']['height'] . '</height>';
$this -> RSSbuffer .= ' </image>';
}
//if (!empty($this -> RSSarr['rating'])) Comment marche cette proriete ?
//$this -> RSSbuffer .= ' <rating>' . $this -> RSSarr['rating'] . '</rating>';
if (!empty($this -> RSSarr['textInput'])) {
$this -> RSSbuffer .= ' <textInput>';
if (!empty($this -> RSSarr['textInput']['title']))
$this -> RSSbuffer .= ' <title>' . $this -> RSSarr['textInput']['title'] . '</title>';
if (!empty($this -> RSSarr['textInput']['description']))
$this -> RSSbuffer .= ' <description>' . $this -> RSSarr['textInput']['description'] . '</description>';
if (!empty($this -> RSSarr['textInput']['name']))
$this -> RSSbuffer .= ' <name>' . $this -> RSSarr['textInput']['name'] . '</name>';
if (!empty($this -> RSSarr['textInput']['link']))
$this -> RSSbuffer .= ' <link>' . $this -> RSSarr['textInput']['link'] . '</link>';
$this -> RSSbuffer .= ' </textInput>';
}
if (!empty($this -> RSSarr['skipHours']))
$this -> RSSbuffer .= ' <hour>' . $this -> RSSarr['skipHours'] . '</hour>';
if (!empty($this -> RSSarr['skipDays']))
$this -> RSSbuffer .= ' <day>' . $this -> RSSarr['skipDays'] . '</day>';
}
/**
* @desc Add item in the RSS Feed.
*
* @param array A element
* @param array A ArgumentsArr
*
* @access public
* @since 1.0
*/
public function AddItem($element, $ArgumentsArr) {
if (!array_key_exists ($element, $this -> RSSarr))
throw new Exception ($element . ' n\'est pas un élément RSS valide');
foreach ($this -> RSSarr[$element] as $key => $value) {
if (array_key_exists ($key, $ArgumentsArr))
$this -> RSSarr[$element][$key] = $ArgumentsArr[$key];
else if (!empty ($value))
$this -> RSSarr[$element][$key] = $value;
}
if (!empty($this -> RSSarr['item'])) {
$this -> RSSbuffer .= ' <item>';
if (!empty($this -> RSSarr['item']['title']))
$this -> RSSbuffer .= ' <title>' . $this -> RSSarr['item']['title'] . '</title>';
if (!empty($this -> RSSarr['item']['link']))
$this -> RSSbuffer .= ' <link>' . $this -> RSSarr['item']['link'] . '</link>';
if (!empty($this -> RSSarr['item']['description']))
$this -> RSSbuffer .= ' <description>' . $this -> RSSarr['item']['description'] . '</description>';
if (!empty($this -> RSSarr['item']['author']))
$this -> RSSbuffer .= ' <author>' . $this -> RSSarr['item']['author'] . '</author>';
if (!empty($this -> RSSarr['item']['category']))
$this -> RSSbuffer .= ' <category domain="' . $this -> RSSarr['item']['category']['domain'] . '">' . $this -> RSSarr['item']['category']['category'] . '</category>';
if (!empty($this -> RSSarr['item']['comment']))
$this -> RSSbuffer .= ' <comments>' . $this -> RSSarr['item']['comments'] . '</comments>';
if (!empty($this -> RSSarr['item']['enclosure']))
$this -> RSSbuffer .= ' <enclosure url="' . $this -> RSSarr['item']['enclosure']['url'] . '" length="' . $this -> RSSarr['item']['enclosure']['length'] . '" type="' . $this -> RSSarr['item']['enclosure']['type'] . '" />';
if (!empty($this -> RSSarr['item']['guid']))
$this -> RSSbuffer .= ' <guid isPermaLink="' . $this -> RSSarr['item']['guid']['isPermaLink'] . '">' . $this -> RSSarr['item']['guid']['guid'] . '</guid>';
if (!empty($this -> RSSarr['item']['pubDate']))
$this -> RSSbuffer .= ' <pubDate>' . $this -> RSSarr['item']['pubDate'] . '</pubDate>';
if (!empty($this -> RSSarr['item']['source']))
$this -> RSSbuffer .= ' <source url="' . $this -> RSSarr['item']['source']['url'] . '">' . $this -> RSSarr['item']['source']['source'] . '</source>';
$this -> RSSbuffer .= ' </item>';
}
}
/**
* @desc Close RSS Feed.
*
* @access public
* @since 1.0
*/
public function CloseFeed() {
$this -> RSSbuffer .= '</channel>';
$this -> RSSbuffer .= '</rss>';
}
/**
* @desc Save RSS Feed in a XML file.
*
* @access public
* @since 1.0
*/
public function SaveFeed() {
if (@is_file($this -> RSSpath . $this -> RSSname))
@unlink($this -> RSSpath . $this -> RSSname);
$fopen = @fopen($this -> RSSpath . $this -> RSSname, 'w+');
if (!$fopen)
throw new Exception('Impossible d\'ouvrir un fichier' . $this -> RSSpath . $this -> RSSname);
fwrite($fopen, $this -> RSSbuffer);
fclose($fopen);
}
/**
* @desc Show RSS Feed.
*
* @return string RSSbuffer content the RSS feed
*
* @access public
* @since 1.0
*/
public function __toString() {
header('Content-type: text/xml');
return $this -> RSSbuffer;
}
/**
* @desc destruct RSS200 Object instance.
*
* @access public
* @since 1.0
*/
public function __destruct () {
unset ($this);
}
}
?>
EXEMPLE D UTILISATION
<?php
include_once('RSS200.class.php');
// try to create RSS Feed.
try {
$FeedPath = ''; // Directory where the XML file save, WITH '/' at the end.
$FeeName = ''; // Name of the RSS Feer, for exemle 'news' NOT 'new.xml' !
// new RSS200 Object instance.
$rss = new RSS200($FeedPath, $FeeName);
// Open RSS Feed.
$rss -> OpenFeed(array( 'encoding' => 'utf-8',
'stylesheet' => '',
'version' => '2.0',
'title' => 'MonSite',
'link' => 'http://www.MonSite.com',
'description' => 'Mon Super site !',
'language' => 'fr-FR',
'copyright' => 'Copyright by Moi',
'managingEditor' => 'Moi',
'webMaster' => 'Moi',
'pubDate' => '',
'lastBuildDate' => '',
'category' => array ('category' => '',
'domain' => ''),
'generator' => 'RSS generated by Quentin Berlemont\'s RSS200 Class', // LESS IT, THANX.
'docs' => 'http://blogs.law.harvard.edu/tech/rss', // FULL DOCUMENTATION ABOUT XML (en) AND http://www.stervinou.com/projets/rss/ (fr).
'cloud' => array('domain' => '',
'port' => '',
'path' => '',
'registerProcedure' => '',
'protocol' => ''),
'ttl' => '',
'image' => array('url' => '',
'title' => '',
'link' => '',
'width' => '88',
'height' => '31'),
'rating' => '',
'textInput' => array('title' => '',
'description' => '',
'name' => '',
'link' => ''),
'skipHours' => '', // Number between 0 and 23.
'skipDays' => '', // Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday.
)
);
$rss -> AddItem('item', array('title' => 'test 1',
'link' => '',
'description' => ' description 1',
'author' => '',
'category' => array ('category' => '',
'domain' => ''),
'comments' => '',
'enclosure' => array('url' => '',
'length' => '',
'type' => ''),
'guid' => array('guid' => '',
'isPermaLink' => ''),
'pubDate' => '',
'source' => array('source' => '',
'url' => '')
)
);
// Close RSS Feed, DON't FORGET IT !
$rss -> CloseFeed();
echo $rss; // Show RSS Feed.
//$rss -> SaveFeed(); // Save RSS Feed in a XML file.
}
//if Error, show it
catch(Exception $e) {
echo '<pre>', print_r($e), '</pre>';
}
?>
Conclusion
Future amelioration, ajouter une méthode pour parser les fichiers XML distant et elle sera complète (je pense)
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Recherche de flux xml ou/et rss de prog tv sportif [ par bobganjx ]
Pauvre de moi en stage, exploité par un employeur sans scrupule qui me paye pas (c'est beaux les études) ;<font color="#800080"
flux rss [ par atchoumen ]
Bonjour,je suis en train de mettre un flux rss en place sur mon site et je me retrouve face à un problème bizarre. Lorsque je fais une requete sans co
widget+rss+php [ par lsamsoumal ]
Bonjour tt le monde: j'ai un code php qui génére un fichier xml contenant des flux rss.Maintenant je veut developper un widget qui récupére les flux r
widget avec flux rss [ par pandouta08 ]
j'ai un code php d'un générateur d'un fichier xml contenant un ensemble de lien et j'ai un widget yahoo que j'ai créé ,je veux que mon widget yahoo af
flux rss [ par mahamourta ]
bonsoir tout le monde je veux installer un flux rss sur mon site web . J'ai reussi a avoir un code dans mon fichier xml ,mais je n'arrive pas a l'exp
je n arrive pas a metre mon flux rss a jour [ par dinadina83 ]
que dieu vous benisse
xml et fusionchart [ par yatoo ]
Bonjour à tous , Je vous contact car j'aimerais créer un graphique avec mon flux xml et fusionchart.( http://www.fusioncharts.com/ ) Mon flux se com
insertion des flux RSS dans des pages php [ par hadjiphp ]
Bonjour, merci pour ce site et souhaite de trouver tous ce que je veux. je vous remercie.je suis entrain de créer un site web en php, et je souhai
Flux RSS [ par hadjiphp ]
Bonjour, merci pour ce site et souhaite de trouver tous ce que je veux. je vous remercie. Bonjour, j'ai une question : comment je peut insérer u
probleme de flux xml [ par stomy95 ]
Bonjour ,j'ai un souci pour afficher du flux xml après 2 jours de recherche j'ai toujours pas résolu mon problème sa m'affiche cette phrase :Ce fichi
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|