Forum Clubic

Problème parser xml rss

Salut , voila j ai récupérer un code php qui me permet d’afficher les flux xml et rss avec enclosure .

il fonctionne très bien mais avec certain flux j ai une erreur ( voir erreur ci dessous )


Warning: PodcastItem::__construct() [podcastitem.--construct]: Node no longer exists in /home/servermick/www/test/Podcast.php on line 95
Warning: PodcastItem::__construct() [podcastitem.--construct]: Node no longer exists in /home/servermick/www/test/Podcast.php on line 96
Warning: PodcastItem::__construct() [podcastitem.--construct]: Node no longer exists in /home/servermick/www/test/Podcast.php on line 97

Voici mon code php


<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

//
// $Id$
class Podcast {
	private $feed_url;
	private $items;
	private $channel;
	
	function __construct() 
	{
		$this->channel 	= array();
		$this->items	= array();
	}
	/**
	* Gets the feed and parses it
	*/
	function parseFeed($url)
	{
		$this->feed_url = $url;
		$file = file_get_contents($url);
		$xml_parser = simplexml_load_string($file);
		$this->channel = array(							// Maps SimpleXML elements into the object
			'title'=>(string) $xml_parser->channel->title,
			'link'=> (string) $xml_parser->channel->link,
			'description'=>(string) $xml_parser->channel->description,
			'pubDate'=>(string) $xml_parser->channel->pubDate
		);
	
		foreach ($xml_parser->channel->children()->item as $item) 
		{					// pushes on PodcastItem objects into the items array for each <item> tag
			$this->items[]  = new PodcastItem($item);
		}
		unset($xml_parser);
		return true;
	}
	
	function getChannelInfo()
	{
		return $this->channel;
	}
	
	function getItems()
	{
		return $this->items;
	}
	/**
		Resets the object ready to parse another feed.
	*/
	function reset()
	{
		$this->__construct();
	}
	
	function getHash()
	{
		return md5(serialize($this));
	}
}

class PodcastItem {
	public $metadata;
	public $enclosure;
	/**
		Converts from SimpleXMLElement to a normal user object
		This means you can serialise/cache it if you wish
	*/
	function __construct($xml_obj)
	{
		$this->metadata = array(
			'title'=>		(string) $xml_obj->title,
			'description'=>	(string) $xml_obj->description,
			'link'=>		(string) $xml_obj->link,
			'guid'=>		(string) $xml_obj->guid,
			'pubDate'=>		(string) $xml_obj->pubDate,
		);
		$enc_tmp =  $xml_obj->enclosure->attributes();
		$this->enclosure = array(
			'url'=> 		(string) $enc_tmp->url,
			'length'=> 		(string) $enc_tmp->length,
			'type'=> 		(string) $enc_tmp->type,
		);
	}
}
?>

Edité le 12/03/2010 à 18:22

Salut,
voila le bon code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  <html> <head>  <title>Création et lecture de flux RSS 2.0 en PHP</title> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta name="robots" content="All"/> <meta name="keywords" content=""/>  <link rel="stylesheet" href="exemple.css" type="text/css"/>  </head>  <body>  <? // Lecture d'un fichier XML function lit_rss($fichier,$champs) {  // on lit le fichier  if($chaine = @implode("",@file($fichier))) {  // on explode sur <item>  $tmp = preg_split("/<\/?"."item".">/",$chaine);  // pour chaque <item>  for($i=1;$i<sizeof($tmp)-1;$i+=2)  // on lit les champs demand? <champ>  foreach($champs as $champ) {  $tmp2 = preg_split("/<\/?".$champ.">/",$tmp[$i]);  // on ajoute au tableau  $tmp3[$i-1][] = @$tmp2[1];  }  // et on retourne le tableau  return $tmp3;  } }  $rss = lit_rss("http://p.yimg.com/dj/rss/technologies.xml",array("title","link","description","pubDate",)); // et on affiche... foreach($rss as $tab) {  echo '<div class="news_box">  <div class="news_box_title">'.$tab[0].'</div>    '.$tab[2].' <a href="'.$tab[1].'">Lire tout l\'article</a>  </div>'; }   ?>  </body> </html>

Accessoirement, quelques topics plus bas, tu as www.clubic.com…

Avec un code qui semble fonctionner, sans pour autant être bien paramétrable.