tutoriel-php-mysql:php:script-recuperation-flux-rss-atom

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

<?php
function RSS_Tags($item, $type)
{
	global $Common_Content;
 
	$y = array();
	$y['title'] = $item->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 .="</ul>\n";
				$opened = false;
			}
			//$page .="<b>";
		}
		else
		{
			if($opened == false && $chanopt == true) 
			{
				$page .= "<ul>\n";
				$opened = true;
			}
		}
		$title = iconv('UTF-8','ISO-8859-15//TRANSLIT',$article['title']);
		$link = $article['link'];
		$page .= "<".$Common_Style."><a href=\"$link\">$title</a>";
 
		if($descopt != false)
		{
			$description = $article['description'];
			if($description != false)
			{
				$page .= "<br>$description";
			}
		}	
		if($dateopt != false)
		{			
			$updated = $article['updated'];
			if($updated != false)
			{
				$page .= "<br /><font $Common_Date_Font>$updated</font>";
			}
		}	
		$page .= "</".$Common_Style.">\n";			
 
		/*
		if($type == 0)
		{
			$page .="<br />";
		}
		*/
	}
 
	if($opened == true)
	{	
		$page .="</ul>\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;
}
?>

Suffit de copier les fonctions ci-dessus dans un fichier functions.php, puis de créer un fichier exemple.php avec ces quelques lignes.

<?php
include_once ('functions.php');
$tableau = array();
// Liste des flux RSS
$url_rss[0] = 'http://www1.zonewebmaster.eu/feed.php';
$url_rss[1] = 'http://www.chg-web.org/backend.php';
foreach ($url_rss as $k=>$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
  • [27/12/2015] : Article initial
  • [17/11/2017] : Harmonisation des pages
  • tutoriel-php-mysql/php/script-recuperation-flux-rss-atom.txt
  • Dernière modification : 08/10/2019 17:03
  • de 127.0.0.1