Tom's Guide > Forum > Programmation > Lecteur RSS Complet
Lecteur RSS Complet - Programmation
TomsGuide.com : 800 000 inscrits répondent à toutes vos questions high-tech et informatique. Pour obtenir de l'aide, inscrivez-vous gratuitement !
Mot :    Pseudo :           
 

Salut, :hello:

j'ai un script Php de lecture de contenu de flux rss/Atom(tout le contenu) du type FeedBurener :love: que j'ai installer sur mon annuaire seulement j'ai un problème c'est qand il sagit d'afficher des caracteres speciaux il m'affiche des symboles du type : ’y a… ù ....
exemple sur la page :
http://guide-oueb.evoconcept.net/r [...] .com/feed/

Voici le Code Source de ma page rss-site.php :

Code :
  1. <?
  2. if(isset($_GET["adresse"])) {
  3. $adresse = ($_GET["adresse"]);
  4. if (preg_match("#^http://.([\w./-]+)$#i",$adresse,$ret)) {
  5. $fil = $ret[0];
  6. // fonctions de nettoyage du texte rŽcupŽrŽ
  7. function clean_text($text) {
  8.     $text = str_replace('&lt;','<',$text);
  9.     $text = str_replace('&gt;','>',$text);
  10.     $text = str_replace('&quot;','"',$text);
  11.     $text = str_replace('&amp;','&',$text);
  12.     if ($encodage == 'utf-8') {
  13.         $text = utf8_decode($text);
  14.     }
  15.     return $text;
  16. }
  17. function clean_date($date)
  18. {
  19.     // si 'AAAA-MM-JJTHH:MM:SS+00:00' ou 'AAAA-MM-JJTHH:MM:SSZ'
  20.     if (ereg("^[0-9]",$date) and  ereg("(([[:digit:]]|-)*)T(([[:digit:]]|:)*)[^[:digit:]].*",$date,$temp)) {
  21.         $date = $temp[1].' '.$temp[3];
  22.     }
  23.     $date = date('d/m/Y', strtotime($date));
  24.     return $date;
  25. }
  26. include 'rss_read.inc.php';
  27. // crŽer l'instance
  28. $rss = new rss_read();
  29. // caractres parasites pouvant tre contenus dans le fils rss et ˆ remplacer par d'autres, doit tre invoquŽ avant parsefile
  30. // cette fonction est optionnelle et ne doit tre utilisŽe que pour certains fils.
  31. $avant = array("'","&bull;" );
  32. $apres = array("'",'-');
  33. $rss -> to_replace_with($avant, $apres);
  34. // parser le fichier news avec Žventuellement un nombre max
  35. $rss -> parsefile($fil,20);
  36. if (!$rss) {
  37.     exit('</a>Fichier rss incorrect !<sup>[<a href="#note1">1</a>]</sup>');
  38. }
  39. // recupŽration de l'encodage du fil iso ou utf vide si celui-ci n'est pas indiquŽ
  40. $encode = $rss -> get_encoding();
  41. // recupŽration des donnŽes sur le channel
  42. $channel = $rss -> get_channel();
  43. }
  44. ?>
  45. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  46. <html>
  47. <head>
  48. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  49. <title>flux <? echo clean_text($channel['title'], $encode);?> sur l'annuaire generaliste gratuit guide-oueb</title>
  50. <meta name="description" content="nouveautés du site <? echo $_GET["adresse"];?> sur l'annuaire generaliste gratuit guide-oueb referencement de flux rss automatique gratuit." />
  51. <meta name="keywords" content="rss, <? echo $_GET["adresse"];?>, flux rss, actualites, nouveautes, guide-oueb" />
  52. <meta name="language" content="fr" />
  53. <meta name="robots" content="all | follow | index" />
  54. <meta name="rating" content="General" />
  55. <meta name="expires" content="never" />
  56.   <style type="text/css">
  57. <!--
  58. .Style1 {color: #FFFFFF}
  59. -->
  60.   </style>
  61. </head>
  62. <body style="background-color: rgb(235, 235, 235);" leftmargin="0" topmargin="0" link="#5c5c5c" marginheight="0" marginwidth="0">
  63. <table class="bg" border="0" cellpadding="0" cellspacing="0" height="98" width="100%">
  64.   <tbody>
  65.     <tr>
  66.       <td>
  67.       <table align="center" border="0" cellpadding="0" cellspacing="0" width="772">
  68.         <tbody>
  69.           <tr>
  70.             <td align="center" bgcolor="#FF0000"><table border="0" cellpadding="0" cellspacing="0" width="770">
  71.               <tbody>
  72.                 <tr>
  73.                   <td width="770" height="130" align="center" bordercolor="#E0DFE3" bgcolor="#FF0000"><?php include('haut-site.php'); ?>
  74.                     <table border="0" cellpadding="0" cellspacing="0" width="770">
  75.                       <tbody>
  76.                         </tbody>
  77.                       </table>
  78.   <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="771" height="131">
  79.     <param name="movie" value="1.swf">
  80.     <param name="quality" value="high">
  81.     <embed src="1.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="771" height="131"></embed>
  82.   </object></td></tr>
  83.                 <tr>
  84.                   <td align="center" bgcolor="#FF0000">
  85. <?php include('menu.php'); ?></td>
  86.                 </tr>
  87.               </tbody>
  88.             </table>
  89.             </td>
  90.           </tr>
  91.           <tr>
  92.             <td align="center" bgcolor="#333333" valign="top">
  93.             <table border="0" cellpadding="0" cellspacing="0" width="770">
  94.               <tbody>
  95.                 <tr>
  96.                   <td bgcolor="#5c5c5c" valign="top" width="222"><?php include('menu2.php'); ?><td bgcolor="#ffffff" valign="top" width="548"><?php
  97. //fichier qui contient tout ce qui doit venir avant le code de l'annuaire (menu de gauche....)
  98. include("before.php" );
  99. include ("haut.php" ); //lien divers de l'annuaire (top clics, top votes, ajout,...)
  100. ?>
  101. <?php
  102. if(isset($_GET["adresse"])) {
  103. if ($rss -> exist_image()) {
  104.     $image = $rss -> get_image();
  105.     echo '<table width="100%" border="0"><tr><td width="50%"><h4 align="center"><a href="'.$channel['link'].
  106.         '" target="_blank" class="ext2">'.clean_text($channel['title'], $encode)."</a></h4></td>\n<td><center><a href=\""
  107.         .$image['link'].'" target="_blank" class="ext"><img src="'.$image['url'].
  108.         '" border="0" alt="'.$image['title']."\"></a></center></td></tr></table>\n"; }
  109. else { echo '<h4 align="center"><a href="'.$channel['link'].'" target="_blank" class="ext2">'.clean_text($channel['title'], $encode)."</a></h4>\n"; }
  110. // nombre d'items   
  111. $nbnews = $rss -> get_num_items();
  112. // recup array des donnŽes
  113. $items = $rss -> get_items();
  114. // pour affichage des dates au format franais
  115. echo "<ul>\n";
  116. for($i = 0; $i < $nbnews; $i++) {
  117.     echo '<li>';
  118.     if (trim($items[$i]['pubdate'])!='') {
  119.         echo '<b>',clean_date(trim($items[$i]['pubdate'])),'&nbsp;:</b> ';
  120.     }
  121.     if (trim($items[$i]['author'])!='') {
  122.         echo '<span class="vert">',clean_text($items[$i]['author'], $encode),'&nbsp;:</span> ';
  123.     }
  124.     elseif (trim($items[$i]['dc:creator'])!='') {
  125.         echo '<span class="vert">',clean_text($items[$i]['dc:creator'], $encode),'&nbsp;:</span> ';
  126.     }
  127.     echo '<a class="ext" href="',$items[$i]['link'],'" target="_blank">',clean_text($items[$i]['title'], $encode),'</a>';
  128.     if (trim($items[$i]['category'])!="" ) { echo " <b>(".clean_text($items[$i]['category'], $encode)." )</b>"; }
  129.     if (trim($items[$i]['description'])!='') {
  130.         echo '&nbsp;:<br />'.clean_text($items[$i]['description'], $encode);
  131.     }
  132.     echo "</li>\n";
  133. }
  134. echo "</ul>\n";
  135. }
  136. else {echo "<p class=\"rouge\">L'adresse du flux n'est pas valide.<br />";}
  137. }
  138. ?>
  139. <br /><?php echo $nouv ?>
  140. <center><a name="note1"></a><ol>
  141. <li><strong>En cas de difficult&eacute;, il est possible que le fichier ne soit pas conforme. Pour le v&eacute;rifier, voyez <a href="http://feedvalidator.org/" target="_blank" style="color:#FF6600;">Feed Validator</a> ou <a href="http://rss.scripting.com/" target="_blank" style="color:#FF6600;">RSS Validator</a>.</strong></li>
  142.         </ol>
  143. </center>
  144. <?
  145. include ("bas.php" ); //pied de la page
  146. //fichier qui contient tout ce qui doit venir avant le code de l'annuaire (menu de droite....)
  147. include("after.php" );
  148. ?>
  149. </td>
  150.                 </tr>
  151.               </tbody>
  152.             </table>
  153.             </td>
  154.           </tr>
  155.           <tr>
  156.             <td style="background-color: rgb(255, 0, 0);" align="center" bgcolor="#333333" valign="top"><?php include('bas-site.php'); ?></td>
  157.           </tr>
  158.         </tbody>
  159.       </table>
  160.       </td>
  161.     </tr>
  162.     <tr>
  163.       <td align="center" height="40" valign="bottom"><? include('copyrite.php');?></td>
  164.     </tr>
  165.   </tbody>
  166. </table>
  167. </body>
  168. </html>



et Voici le code source de ma page rss_read.inc.php

Code :
  1. <?php
  2. // ------------------------------------------------------
  3. // rss_read - classe pour lire des fichiers RSS/RDF/Atom
  4. // Dominique WOJYLAC <http://wojylac.free.fr>
  5. // Distribué sous GNU General Public License.
  6. //
  7. // Version 2.1c
  8. // -------------------------------------------------------
  9. class rss_read
  10. {
  11.     var $class_version;
  12.     var $rss_channel;
  13.     var $rss_image;
  14.     var $rss_items;
  15.     var $rss_textinput;
  16.     var $rss_encoding;
  17.     var $to_replace;
  18.     var $replace_with;
  19.     var $mode_agrege;
  20.     /*
  21.     ** initialisation de la classe
  22.     */
  23.     function rss_read()
  24.     {
  25.         $this -> class_version = '2.1c';
  26.         $this -> rss_channel = array();
  27.         $this -> rss_image = array();
  28.         $this -> rss_items = array();
  29.         $this -> rss_textinput = array();
  30.         $this -> rss_encoding = '';
  31.         $this -> to_replace = array();
  32.         $this -> replace_with = array();
  33.         $this -> mode_agrege = true;
  34.     }
  35.     /*
  36.     ** initialise le mode
  37.     */
  38.     function mode_brut()
  39.     {
  40.         $this -> mode_agrege = false;
  41.     }
  42.     /*
  43.     ** initialise les tableau pour remplacement de caractères avant traitement du flux
  44.     */
  45.     function to_replace_with($avant, $apres)
  46.     {
  47.         if (!empty($avant)) {
  48.             $this -> to_replace = $avant;
  49.         }
  50.         if (!empty($apres)) {
  51.             $this -> replace_with = $apres;
  52.         }
  53.     }
  54.     /*
  55.     ** analyse les entêtes pour trouver Last-Modified
  56.     ** retourne la donnée sous forme timestamp ou telle quelle
  57.     */
  58.     function header_last_modified($headers, $format = true) {
  59.         if (preg_match('/HTTP\/1.1 404/', $headers)) {
  60.             return false;
  61.         }
  62.         else if (preg_match("/Last-Modified: ([^\r]*)\r/i", $headers ,$temp)) {
  63.             $date_modif = $temp[1];
  64.         }
  65.         else {
  66.             $date_modif = '';
  67.         }
  68.         if ($format == true) {
  69.             if (empty($date_modif)) {
  70.                 return 0;
  71.             }
  72.             else {
  73.                 return strtotime($date_modif);
  74.             }
  75.         }
  76.         else {
  77.             return $date_modif;
  78.         }
  79.     }
  80.     /*
  81.     ** date de modification avec fsockopen
  82.     */
  83.     function get_last_modified($url = '', $format = true) {
  84.         $url_array = parse_url($url);
  85.         $host = $url_array['host'];
  86.         $path = $url_array['path'];
  87.         if (empty($url) or empty($host) or empty($path)) {
  88.             return false;
  89.         }
  90.         $fp = @fsockopen($host, 80);
  91.         if (!$fp) {
  92.           return false;
  93.         }
  94.         $out = 'HEAD '.$path.' HTTP/1.1'."\r\n".
  95.             'Host: '.$host."\r\n".
  96.             'Connection: Close'."\r\n\r\n";
  97.         $lines = '';
  98.         fwrite($fp, $out);
  99.         while (!feof($fp)) {
  100.             $lines .= fgets($fp, 1024);
  101.         }
  102.         fclose($fp);
  103.         if (empty($lines)) {
  104.             return false;
  105.         }
  106.         else {
  107.             return $this -> header_last_modified($lines, $format);
  108.         }
  109.     }// fin gest_last_modified
  110.     /*
  111.     ** date de modification avec bibliotheque curl
  112.     */
  113.     function curl_get_last_modified($url = '', $format = true) {
  114.         if (empty ($url)) {
  115.             return false;
  116.         }
  117.         $ch = curl_init($url);
  118.         // curl_setopt($ch, CURLOPT_URL, $url);
  119.         curl_setopt($ch, CURLOPT_HEADER, true);
  120.         curl_setopt($ch, CURLOPT_NOBODY, true);
  121.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  122.         $lines = curl_exec ($ch);
  123.         curl_close ($ch);
  124.         if (empty($lines)) {
  125.             return false;
  126.         }
  127.         else {
  128.             return $this -> header_last_modified($lines, $format);
  129.         }
  130.     }
  131.     /*
  132.     ** analyse le flux
  133.     */
  134.     function parse_data($data, $max_item = 0)
  135.     {
  136.         if (preg_match('/encoding="([^"]*)"/i', $data ,$temp)) {
  137.             $this -> rss_encoding = strtolower($temp[1]);
  138.         }
  139.         // remplacement eventuel
  140.         if (!empty($this -> to_replace) and !empty($this -> replace_with)) {
  141.             $data = str_replace($this -> to_replace, $this -> replace_with, $data);
  142.         }
  143.         $xml_parser = xml_parser_create();
  144.         xml_parse_into_struct($xml_parser, $data, $vals);
  145.         $parent = array();
  146.         $n_item = 0;
  147.         // RSS ou FEED
  148.         $fil_type = $vals[0]['tag'];
  149.         // $vals = tableau associatif déterminé par la structure du fil
  150.         //print_r($vals);
  151.         foreach ($vals as $row) {
  152.             // $row = tableau associatif correspondant à une ligne de $vals
  153.             // 4 indices : tag, type, level, value
  154.             foreach ($row as $key => $val) {
  155.                 ${$key} = $val;
  156.             }
  157.             $tag = strtolower($tag);
  158.             switch ($type) {
  159.             case 'open' :
  160.                 $parent[$level] = $tag ;
  161.                 if (($tag == 'item') or ($tag == 'entry')) {
  162.                     $n_item++;
  163.                 }
  164.             break;
  165.             case 'close' :
  166.                 if ($parent[$level] == $tag) {
  167.                     $parent[$level] = '';
  168.                 }
  169.             break;
  170.             case 'complete' :
  171.                 // cas specifiques à atom
  172.                 if (($fil_type == 'FEED') and ($tag == 'link')) {
  173.                     $value = $attributes['HREF'];
  174.                 }
  175.                 if (($fil_type == 'FEED') and ($tag == 'category')) {
  176.                     $value = $attributes['label'];
  177.                 }
  178.                 // cas non spécifiques
  179.                 if ($this -> mode_agrege) {
  180.                 switch ($tag) {
  181.                     case 'dc:subject' :
  182.                         $tag = 'category';
  183.                     break;
  184.                     case 'date':
  185.                     case 'dc:date':
  186.                     case 'published':
  187.                     case 'issued':
  188.                         $tag = 'pubdate';
  189.                     break;
  190.                     case 'updated':
  191.                         $tag = 'modified';
  192.                     break;
  193.                     case 'dc:description':
  194.                     case 'summary':
  195.                     case 'content:encoded':
  196.                         $tag = 'description';
  197.                     break;
  198.                     case 'dc:rights':
  199.                     case 'rights':
  200.                         $tag = 'copyright';
  201.                     break;
  202.                     case 'dc:creator':
  203.                         $tag = 'author';
  204.                     break;
  205.                 } // fin switch
  206.                 } // fin if mode agrege
  207.                 switch ($parent[$level - 1]) {
  208.                     case 'feed':
  209.                     case 'channel':
  210.                         $this -> rss_channel[$tag] = $value;
  211.                     break;
  212.                     case 'entry':
  213.                     case 'item':
  214.                         if(($n_item <= $max_item) or ($max_item == 0)) {
  215.                             $this -> rss_items[$n_item - 1][$tag] = $value;
  216.                         }
  217.                     break;
  218.                     case 'image':
  219.                         $this -> rss_image[$tag] = $value;
  220.                     break;
  221.                     case 'textinput':
  222.                         $this -> rss_textinput[$tag] = $value;
  223.                     break;
  224.                 } // fin switch $parent
  225.             break;
  226.             } // fin switch $type
  227.         } // fin foreach
  228.         xml_parser_free($xml_parser);
  229.     } // fin parse_data
  230.     /*
  231.     ** ouvrir et lire le flux avec fopen
  232.     */
  233.     function parsefile($filename, $max_item = 0) {
  234.         if(!$fp = @fopen($filename, 'r')) {
  235.             return false;
  236.         }
  237.         $lines = '';
  238.         while (!feof($fp)) {
  239.             $lines .= fread($fp, 4096);
  240.         }
  241.         @fclose($fp);
  242.         // ou php >= 4.3
  243.         // $lines = file_get_contents($filename);
  244.         if (empty($lines)) {
  245.             return false;
  246.         }
  247.         else {
  248.             $this -> parse_data($lines, $max_item, $mode);
  249.             return true;
  250.         }
  251.     } // fin parsefile
  252.     /*
  253.     ** ouvrir et lire le flux avec bibliotheque curl
  254.     */
  255.     function curl_parsefile($filename ='', $max_item = 0) {
  256.         if (empty($filename)) {
  257.             return false;
  258.         }
  259.         $lines = '';
  260.         $ch = curl_init($filename);
  261.         // curl_setopt($ch, CURLOPT_URL, $filename);
  262.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  263.         $lines = curl_exec ($ch);
  264.         curl_close ($ch);
  265.         if (empty($lines)) {
  266.             return false;
  267.         }
  268.         else {
  269.             $this -> parse_data($lines, $max_item, $mode);
  270.             return true;
  271.         }
  272.     } // fin curl_parsefile
  273.     // encoding
  274.     function get_encoding() {
  275.         return $this -> rss_encoding;
  276.     }
  277.     // channel
  278.     function exist_channel() {
  279.         return (!empty($this -> rss_channel) and
  280.             !empty($this -> rss_channel['title']) and
  281.             !empty($this -> rss_channel['link']));
  282.     }
  283.     function get_channel() {
  284.         return $this -> rss_channel;
  285.     }
  286.     // image
  287.     function exist_image() {
  288.         return (!empty($this -> rss_image['url']));
  289.     }
  290.     function get_image() {
  291.         return $this -> rss_image;
  292.     }
  293.     // items
  294.     function exist_items() {
  295.         return (count($this -> rss_items) > 0);
  296.     }
  297.     function get_items() {
  298.         return $this -> rss_items;
  299.     }
  300.     function get_num_items() {
  301.         return count($this -> rss_items);
  302.     }
  303.     // input
  304.     function exist_textinput() {
  305.         return (!empty($this -> rss_textinput) and
  306.             !empty($this -> rss_textinput['title']) and
  307.             !empty($this -> rss_textinput['link']) and
  308.             !empty($this -> rss_textinput['description']) and
  309.             !empty($this -> rss_textinput['name']));
  310.     }
  311.     function get_textinput() {
  312.         return $this -> rss_textinput;
  313.     }
  314. }
  315. ?>



Je vous prie de bien vouloir m'aider a trouver une solution.

Merci d'avance .

------------------------------ http://persocite.francite.com/yassineaniber/
Liens sponsorisés
Inscrivez-vous ou connectez-vous pour masquer ceci.
- 0 +

Salut,

C'est un problème d'encodage.
Tu fais un utf8-decode si ta variable $encodage == 'utf8', alors que ta variable $encodage n'est définie nulle part, donc tu ne feras jamais ce utf8-decode.

Répondre à OmaR

j'ai fait cette modification a la fonctionclean_text() et sa marche mieux

Code :
  1. function clean_text($text,$encodage) {
  2.     $text = str_replace('&lt;','<',$text);
  3.     $text = str_replace('&gt;','>',$text);
  4.     $text = str_replace('&quot;','"',$text);
  5.     $text = str_replace('&amp;','&',$text);
  6.     if ($encodage == 'utf-8') {
  7.         $text = utf8_decode($text);
  8.     }
  9.     return $text;
  10. }



http://guide-oueb.evoconcept.net/r [...] .com/feed/

sauf pour les caracteres ' et quelques autres ...

Merci pour votre aide


Message édité par yassine123 le 11-01-2008 à 14:20:02
------------------------------ http://persocite.francite.com/yassineaniber/
Répondre à yassine123
Tom's Guide > Forum > Programmation > Lecteur RSS Complet
Aller à :

Il y a 2774 utilisateurs connus et inconnus. Pour voir la liste des connectés connus, cliquez ici.

Attention

Vous allez répondre sur un sujet resté inactif pendant plus de 6 mois.
Assurez-vous d'apporter des éléments nouveaux à la discussion avant de poursuivre.

Répondre Annuler
Liens