Jump to content
Sign in to follow this  
Umbus

Leitor de feeds melhorar

Recommended Posts

Umbus

Boas pessoal fiz este leitor de feeds para um trabalho de univ mas não sei se esta muito bem feito ou o que poderia melhorar. Estou aberto a opiniões:

<?php
$feeds = file_get_contents('http://news.google.pt/news?q=politica+sapo&hl=pt-PT&cr=countryPT&prmd=ivnsu&biw=1440&bih=742&um=1&ie=UTF-8&output=rss');
$rss = new SimpleXmlElement($feeds);

foreach($rss->channel->item as $lerfeed) {
	echo $lerfeed->description;
}	
?>

Tambem queria ter opção de varios sites mas nao estou a ver como fazer


Eu sou eu e eu sou aquele que sou eu...

Share this post


Link to post
Share on other sites
faianca

Basta so criares um array com as feeds todas que quiseres e depois fazes um loop entre elas e metes o conteudo dessa feed para um novo array e fazes merge do conteudo de todos nesse mesmo.

<?php
// Array com as feeds
$feeds = array(
    'http://news.google.pt/news?q=politica+sapo&hl=pt-PT&cr=countryPT&prmd=ivnsu&biw=1440&bih=742&um=1&ie=UTF-8&output=rss',
    'http://rss.news.yahoo.com/rss/techblog'
    
);

// loop entre as feeds e organizar o conteudo dentro dum novo array
$entries = array();
foreach ($feeds as $feed) {
    $xml = simplexml_load_file($feed);
    $entries = array_merge($entries, $xml->xpath('/rss//item'));
}

// ordenar feeds por data PubDate
usort($entries, function ($x, $y) { return strtotime($x->pubDate) - strtotime($y->pubDate);});


// Display as noticias
foreach($entries as $entry):
    echo $entry->title;
    echo '<br/>';
    echo $entry->description;
    echo '<br/><br/>';
endforeach;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.