Recorrer archivo XML o RSS Con PHP 4 y 5

27 enero, 2008 por Mr. FotoPex Dejar una respuesta »

 

 

Si tienes alguna duda de que son los archivos Archivo RSSo Feed RSS (o XML), puedes leer primero este artículo.

Ahora a lo otro.
Primero vamos a definir el color de los hipervínculos.

CSS:
  1. <style type="text/css">
  2. <!--
  3. a:link {
  4.     color: #055A91;
  5.     font-weight: bold;
  6.     text-decoration: none;
  7. }
  8. a:visited {
  9.     text-decoration: none;
  10.     font-weight: bold;
  11.     color: #055A91;
  12. }
  13. a:hover {
  14.     text-decoration: none;
  15.     font-weight: bold;
  16.     color:#990000;
  17. }
  18. a:active {
  19.     text-decoration: none;
  20.     font-weight: bold;
  21.     color: #055A91;
  22. }
  23. .linktitle {font-size: large}
  24. -->
  25. </style>

Esta instrucción es para cargar la extensión que utilizaremos, en caso de que no este cargada en la configuración de php.ini.
Si tienes acceso al archivo php.ini, debes de quitar el “;" que tiene. Y quedara extension=php_domxml.dll.
Funciona solamente si tienes php corriendo sobre windows.

PHP:
  1. if (!dl('php_domxml.dll')) { echo 'No se pudo cargar'; }

Ahora vamos a crear la función. que leerá el fichero xml. En este caso leeremos uno creado por php

PHP:
  1. function CargarXML($ruta_fichero){ 
  2.  
  3.     $tagnames = array ("title","link","description","comments"); //creamos el array para los valores que desplegaremos
  4.     if (!$xml = domxml_open_file($ruta_fichero)){
  5.         echo 'error al procesar el documento<strong>'.$ruta_fichero.'</strong> a XML <br>';
  6.         exit;
  7.     }else{
  8.         $raiz = $xml->document_element();
  9.         $tam=sizeof($tagnames);
  10.         for($i=0; $i<$tam; $i++){
  11.             $nodo = $raiz->get_elements_by_tagname($tagnames[$i]);
  12.             $j=0;
  13.             foreach ($nodo as $etiqueta){
  14.                 $matriz[$j][$tagnames[$i]]=$etiqueta->get_content();
  15.                 $j++;
  16.             }
  17.         }
  18.         return $matriz;
  19.     }
  20. }

Finalmente abrimos el fichero con la función. Este ejemplo es para php versión 4 o superior.

PHP:
  1. $matriz=CargarXML("http://www.empresario.com.mx/?feed=rss2");
  2.  
  3. $num_noticias=sizeof($matriz);
  4. for($i=0;$i<$num_noticias;$i++){   
  5.     echo '<a href="';
  6.     echo utf8_decode($matriz[$i]["link"]);
  7.     echo '" class="linktitle">';
  8.     echo utf8_decode($matriz[$i]["title"]);
  9.     echo '</a><hr />';
  10.     echo utf8_decode($matriz[$i]["description"]);
  11.     echo '<a href="';
  12.     echo utf8_decode($matriz[$i]["link"]);
  13.     echo '"> Leer m&aacute;s</a>';
  14.     echo '<br />';
  15.     echo '<a href="';
  16.     echo utf8_decode($matriz[$i]["link"]);
  17.     echo '">';
  18.     echo '<br /><img src="feedcomments.png" border="0" /> Comentarios';
  19.     echo '</a>';
  20.     echo '<br /><br /><br />';
  21. }

 

En el ejemplo para la versión 5 o superior utilizaremos también el formato css del principio.

PHP:
  1. $url="http://www.empresario.com.mx/?feed=rss2"//direccion del fichero
  2.  
  3. $num_noticies=15; //numero de noticias que desplegara
  4. // en caso de que el numero del fichero sea menor no hay problema.
  5.  
  6. $Noticias = simplexml_load_file($url);
  7.  
  8. if (!count((array)$Noticias->item))
  9. {
  10.     for ($i=0; $Noticias->channel->item[$i]->title!="" AND $i<$num_noticies; $i++)
  11.     {
  12.         echo '<a href="';
  13.         echo utf8_decode($Noticias->channel->item[$i]->link);
  14.         echo '" class="linktitle">';
  15.         echo utf8_decode($Noticias->channel->item[$i]->title);
  16.         echo '</a><hr />';
  17.  
  18.         echo '<br />';
  19.         echo utf8_decode($Noticias->channel->item[$i]->description);
  20.         echo '<a href="';
  21.         echo utf8_decode($Noticias->channel->item[$i]->link);
  22.         echo '"> Leer m&aacute;s</a>';
  23.         echo '<br />';
  24.         echo '<a href="';
  25.         echo utf8_decode($Noticias->channel->item[$i]->comments);
  26.         echo '">';
  27.         echo '<br /><img src="feedcomments.png" border="0" /> Comentarios';
  28.         echo '</a>';
  29.         echo '<br /><br /><br />';
  30.     }
  31. }       
  32. else
  33. {
  34.     for ($i=0; $Noticias->channel->item[$i]->title!="" AND $i<$num_noticies; $i++)
  35.     {
  36.         echo utf8_decode($Noticias->item[$i]->title);
  37.         echo '<br />';
  38.     }
  39. }

Publicidad

Deja un comentario