====== Script de récupération des données d'un Flux RSS ou Atom ====== Voici quelques fonctions en PHP permettant la récupération de données présentes dans un flux RSS ou Atom. Ces fonctions retournent un tableau avec la date de publication, le titre, le lien où se trouve l'article et la description. Scripts fonctionnant sur PHP 5 ===== Le script pour la récupération des Flux RSS ou Atom ===== getElementsByTagName('title')->item(0)->firstChild->textContent; $y['link'] = $item->getElementsByTagName('link')->item(0)->firstChild->textContent; $y['description'] = $item->getElementsByTagName('description')->item(0)->firstChild->textContent; $tnl = $item->getElementsByTagName('pubDate'); if($tnl->length == 0) { $tnl = $item->getElementsByTagName('lastBuildDate'); } if($tnl->length != 0) { $tnl =$tnl->item(0)->firstChild->textContent; } else $tnl = false; $y['updated'] = $tnl; $y['type'] = $type; array_push($Common_Content, $y); } function RSS_Channel($channel) { global $Common_Content; $items = $channel->getElementsByTagName('item'); // Processing channel RSS_Tags($channel, 0); // get description of channel, type 0 // Processing articles foreach($items as $item) { RSS_Tags($item, 1); // get description of article, type 1 } } function RSS_Retrieve($url) { global $Common_Content; $doc = new DOMDocument(); $doc->load($url); $channels = $doc->getElementsByTagName('channel'); $Common_Content = array(); foreach($channels as $channel) { RSS_Channel($channel); } return ( count($Common_Content) > 0); } function Atom_Tags($item) { global $Common_Content; $y = array(); $y['title'] = $item->getElementsByTagName('title')->item(0)->firstChild->textContent; $y['link'] = $item->getElementsByTagName('link')->item(0)->getAttribute('href'); $y['description'] = $item->getElementsByTagName('summary')->item(0)->firstChild->textContent; $y['updated'] = $item->getElementsByTagName('updated')->item(0)->firstChild->textContent; $y['type'] = 1; array_push($Common_Content, $y); } function Atom_Feed($doc) { global $Common_Content; $entries = $doc->getElementsByTagName('entry'); if($entries->length == 0) return false; // Processing feed $y = array(); $y['title'] = $doc->getElementsByTagName('title')->item(0)->firstChild->textContent; $y['link'] = $doc->getElementsByTagName('link')->item(0)->getAttribute('href'); $y['description'] = $doc->getElementsByTagName('subtitle')->item(0)->firstChild->textContent; $y['updated'] = $doc->getElementsByTagName('updated')->item(0)->firstChild->textContent; $y['type'] = 0; array_push($Common_Content, $y); // Processing articles foreach($entries as $entry) { Atom_Tags($entry); // get description of article, type 1 } return true; } function Atom_Retrieve($url) { global $Common_Content; $doc = new DOMDocument(); $doc->load($url); $Common_Content = array(); return Atom_Feed($doc); } function Common_Display($url, $size = 25, $chanopt = false, $descopt = false, $dateopt = false) { global $Common_Content; global $Common_Style; global $Common_Date_Font; $opened = false; $page = ''; if(Atom_Retrieve($url) === false) { if(RSS_Retrieve($url) === false){ return 'ERREUR'; } } if($size > 0) { $size += 1; // add one for the channel $recents = array_slice($Common_Content, 0, $size); } foreach($recents as $article){ $type = $article['type']; if($type == 0) { if($chanopt != true) { continue; } if($opened == true){ $page .="\n"; $opened = false; } //$page .=""; } else { if($opened == false && $chanopt == true) { $page .= "\n"; } return $page."\n"; } function liste_rss($url, $size = 25, $chanopt = false, $descopt = false, $dateopt = false) { global $Common_Content; global $Common_Style; global $Common_Date_Font; $opened = false; $tableau = array(); $n = 0; if(Atom_Retrieve($url) === false) { if(RSS_Retrieve($url) === false){ return 'ERREUR'; } } if($size > 0) { $size += 1; // add one for the channel $recents = array_slice($Common_Content, 1, $size); } foreach($recents as $article){ //$tableau[$n]['title'] = iconv('UTF-8','ISO-8859-15//TRANSLIT',$article['title']); $tableau[$n]['title'] = $article['title']; $tableau[$n]['link'] = $article['link']; $tableau[$n]['date'] = $article['updated']; $tableau[$n]['description'] = $article['description']; $n++; } return $tableau; } ?> ===== Comment sa fonctionne ===== Suffit de copier les fonctions ci-dessus dans un fichier functions.php, puis de créer un fichier exemple.php avec ces quelques lignes. $v) { $tableau = array_merge($tableau,liste_rss($v, 25, true, true, true)); } Suffit ensuite d'exploiter le contenu du tableau **$tableau**. Ce contenu se présente de cette façon : $tableau[0]['titre'] //-> le titre de l'info $tableau[0]['link'] //-> le lien où se trouve l'info $tableau[0]['date'] //-> la date de publication $tableau[0]['description'] //-> la description ===== Notes de version ===== * [27/12/2015] : Article initial * [17/11/2017] : Harmonisation des pages ===== Auteurs et sources ===== * Auteur : [[:user:montuy337513]] ===== Navigation ===== * [[:accueil|Accueil]] * [[:gestion-site-internet]] * [[:tutoriel-javascript]] * [[:tutoriel-html]] * [[:tutoriel-php-mysql]] * [[:tutoriel-referencement-seo]]