Inicio > Programación Web > Lector RSS en PHP con cache

Lector RSS en PHP con cache

jueves, 1 de febrero de 2024 Dejar un comentario Ir a comentarios

Con la siguiente función para leer RSS puedes establecer algunos valores:

function rssReader($url,$maxItems=15,$begin="",$end=""){
        $rss = simplexml_load_file($url);
        $i=0;
        if($rss){
            $items = $rss->channel->item;
            foreach($items as $item){
                if($i==$maxItems) return $out;
                else $out.=$begin.'<a href="'.$item->link.'" target="_blank" >'.$item->title.'</a>'.$end;
                $i++;
            }
        }
        return $out;
    }


forma de uso:

echo '<ul>'.rssReader('/feed/',5,'<li>','</li>').'</ul>';

Usando la cache con la clase fileCache:

 function rssReader($url,$maxItems=15,$cache,$begin="",$end=""){
        $cache= (bool) $cache;
        if ($cache){
            $cacheRSS= new fileCache();//seconds and path
            $out = $cacheRSS->cache($url);//getting values from cache
        }else $out=false;
 
        if (!$out) {	//no values in cache
            $rss = simplexml_load_file($url);
            $i=0;
            if($rss){
                $items = $rss->channel->item;
                foreach($items as $item){
                    if($i==$maxItems){
                        if ($cache) $cacheRSS->cache($url,$out);//save cache	
                        return $out; 
                    }
                    else $out.=$begin.'<a href="'.$item->link.'" target="_blank" >'.$item->title.'</a>'.$end;
                    $i++;
                }
            }   		
        }
        return $out;
    }

Forma de uso RSS con cache:

echo '<ul>'.rssReader('/feed/',5,true,'<li>','</li>').'</ul>';

Basado en la forma convencional:

 $url = "http://news.google.com/?ned=us&topic=t&output=rss";
    $rss = simplexml_load_file($url);
    if($rss){
        echo '<h1>'.$rss->channel->title.'</h1>';
        echo '<li>'.$rss->channel->pubDate.'</li>';
        $items = $rss->channel->item;
        foreach($items as $item){
            $title = $item->title;
            $link = $item->link;
            $published_on = $item->pubDate;
            $description = $item->description;
            echo '<h3><a href="'.$link.'">'.$title.'</a></h3>';
            echo '<span>('.$published_on.')</span>';
            echo '<p>'.$description.'</p>';
        }
    }

Comparte y diviertete:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • Blogosphere
  • Live
  • Meneame
  • MSN Reporter
  • MySpace
  • RSS
  • Suggest to Techmeme via Twitter
  • Technorati
  • LinkedIn
  • email
  • FriendFeed
  • PDF
  • Reddit
  • Wikio IT
  • Add to favorites
  • blogmarks
Categories: Programación Web Tags: , ,
Top Footer