Code :
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
- <?php
- /////////////////////////////////////////////////////////////////////////////////
- // PHPRSSReader.php - d'aprs HTML Reader par Jorge Solis
- // traduit et simplifi par Pierre Rouarch pour tenir en un fichier
- ////////////////////////////////////////////////////////////////////////////////
- class xml {
- var $parser;
- function parse($xmlstring){
- $this->parser = xml_parser_create();
- xml_set_object($this->parser, $this);
- xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
- xml_set_element_handler($this->parser, "tag_open", "tag_close" );
- xml_set_character_data_handler($this->parser, "cdata" );
- // Initialisation
- $this->node_stack = array();
- $this->tag_open(null, "root", array());
- xml_parse($this->parser, $xmlstring);
- xml_parser_free($this->parser);
- $rnode = array_pop($this->node_stack);
- return($rnode);
- }
- function tag_open($parser, $tag, $attributes){
- // creation d'un nouveau node...
- $node = array();
- $node["_NAME"] = $tag;
- foreach ($attributes as $key => $value) {
- $node[$key] = $value;
- }
- $node["_DATA"] = "";
- $node["_ELEMENTS"] = array();
- // ajouter le nouveau node la fin de la pile de node
- array_push($this->node_stack, $node);
- }
- function cdata($parser, $cdata){
- // ajouter les donnes au dernier node de la pile
- $lastnode = count($this->node_stack);
- $this->node_stack[$lastnode-1]["_DATA"] .= $cdata;
- }
- function tag_close($parser, $tag){
- // suppression de la pile
- $node = array_pop($this->node_stack);
- $node["_DATA"] = trim($node["_DATA"]);
- // and ajout comme lment la fin de la pile...
- $lastnode = count($this->node_stack);
- array_push($this->node_stack[$lastnode-1]["_ELEMENTS"], $node);
- }
- } // end of class xml
- // recupration de l' adresse du RSS avec le paramtre rssurl
- $rssurl = $HTTP_GET_VARS["rssurl"];
- //////////////////////////////////////////////////////////////////////////////////////////////////
- //Modifiez ici pour mettre l'adresse URL de votre RSS au cas ou aucun argument n'est pass:
- // exemple :
- // http://www.votresite.com/votrefichierrss' target='_blank' rel='nofollow'>http://www.votresite.com/...
- /////////////////////////////////////////////////////////////////////////////////////////////
- if (!$rssurl ) $rssurl = "http://www.fakesite.com/rss/rss.xml";
- // rcupration du fichier par paquets de 4096
- $fp = fopen($rssurl, "r" );
- while (!feof ($fp)) $data .= fgets($fp, 4096);
- fclose ($fp);
- $initPos = strpos($data, "<channel>" ); // Saut de l'entte
- //parse les donnes
- $xml_parser = new xml();
- $data = substr($data, $initPos);
- $root_node = $xml_parser->parse($data);
- $channel = array_shift($root_node["_ELEMENTS"]);
- //Rcupration de certaines donnes du RSS (Voir les specifications rss 0.91, 0.92 et 2.0)
- ////// Rcupration entte de channel /////////////////////////////////////////////////////////
- foreach ($channel["_ELEMENTS"] as $item){
- // suppression des antislashs
- if($item[_NAME]=="title" ) $base_name=stripslashes($item[_DATA]);
- if($item[_NAME]=="link" ) $base_url=stripslashes($item[_DATA]);
- if($item[_NAME]=="description" ) $base_desc=stripslashes($item[_DATA]);
- }
- $base = "<b>Web: </b><a href=\"$base_url\" target=\"_new\">$base_name</a><br>$base_desc<br>";
- /////////////////////////////////////////////////////////////////////////
- ////////// Rcupration de chaque Item --> titre, adresse url, date de publication, description //////////////////////////////////////////////////////
- foreach ($channel["_ELEMENTS"] as $item){
- if($item[_NAME]=="item" ) {
- // suppression des antislashs
- foreach ($item["_ELEMENTS"] as $tag){
- if($tag["_NAME"]=="title" ) $news_title=stripslashes($tag[_DATA]);
- if($tag["_NAME"]=="link" ) $news_url=stripslashes($tag[_DATA]);
- if($tag["_NAME"]=="pubDate" ) $news_pubDate=stripslashes($tag[_DATA]);
- if($tag["_NAME"]=="description" ) $news_desc=stripslashes($tag[_DATA]);
- if($tag["_NAME"]=="itunes:duration" ) $news_duration=stripslashes($tag[_DATA]);
- }
- $news .= "<p>$news_pubDate<br><b>$news_title</b><br>$news_desc<br>$news_duration[0]<br><a href=\"$news_url\" target=\"_new\">$news_url</a><hr></p>";
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- ?>
- <html>
- <head>
- <title>Sample RSS Reader in PHP</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- </head>
- <body>
- <?php print($news) ?>
- <?php print($base) ?>
- </body>
- </html>
|