Función para leer y mostrar un RSS con PHP


/ Published in: PHP
Save to your folder(s)

La siguiente función muestra los títulos, junto con los enlaces, cargados de un RSS.


Copy this code and paste it in your HTML
  1. function lectorRSS($url,$elementos=6,$inicio=0) {
  2. $cache_version = "cache/" . basename($url);
  3. $archivo = fopen($url, 'r');
  4. stream_set_blocking($archivo,true);
  5. stream_set_timeout($archivo, 5);
  6. $datos = stream_get_contents($archivo);
  7. $status = stream_get_meta_data($archivo);
  8. fclose($archivo);
  9. if ($status['timed_out']) {
  10. $noticias = simplexml_load_file($cache_version);
  11. }
  12. else {
  13. $archivo_cache = fopen($cache_version, 'w');
  14. fwrite($archivo_cache, $datos);
  15. fclose($archivo_cache);
  16. $noticias = simplexml_load_string($datos);
  17. }
  18. $ContadorNoticias=1;
  19. echo "<ul>";
  20. foreach ($noticias->channel->item as $noticia) {
  21. if($ContadorNoticias<$elementos){
  22. if($ContadorNoticias>$inicio){
  23. echo "<li><a href='".$noticia->link."' target='_blank' class='tooltip' title='".utf8_decode($noticia->title)."'>";
  24. echo utf8_decode($noticia->title);
  25. echo "</a></li>";
  26. }
  27. $ContadorNoticias = $ContadorNoticias + 1;
  28. }
  29. }
  30. echo "</ul>";
  31. }

Report this snippet


Comments


You need to login to view comments.