PHP library reading and creating RSS
Many articles on the web tend to reveal the death of RSS. I don't agree.
If you see how many users there are on google news or netvibes, you'll see I'm not the only one thinking that way.
Many articles on the web tend to reveal the death of RSS. I don't agree.
If you see how many users there are on google news or netvibes, you'll see I'm not the only one thinking that way.
That's a very smart question, there are so many PHP classes that create or read RSS...
Well, first of all, ShikiryuRSS does both. You can read and create RSS.
ShikiryuRSS is also easy to use. See our documentation or our examples.
ShikiryuRSS follows RSS 2.0 specifications. You can find them here.
Finally, the ability to read and create allows you to take a RSS, modify it and display the new one or save it in your server! Possibilites are increased! :)
First of all, you should document yourself with RSS 2.0 specifications. You can find them here.
A quick overview :
Global informationsYou'll need PHP5 at least
$rss = SRSS::read('http://exemple.com/rss.xml');Easy, right? Then you can extract general informations :
echo $rss->title; // will display blog titleThen, you can take care of articles. You can select a precise article :
$article1 = $rss->getItem(1); // or $rss->getFirst();Or looping them :
foreach($rss as $article) { echo '<a href="'.$article->link.'">'. SRSSTools::formatDate('d/m/y', $item->pubDate).' '.$item->title.''; }If you like arrays, you can transform the RSS into an array :
$rssArray = $rss->toArray();You can also save it into your server with :
$rss->save('/www/rss/rss.xml'); // example
$rss = SRSS::create();Easy, right? Then you can add general informations :
$rss->title = 'My Awesome Blog'; $rss->link = 'http://shikiryu.com/devblog/'; $rss->description = 'is awesome';Those 3 are mandatory to validate your RSS, other options can be added.
$content
contains an array from your database.
foreach($content as $item){ $rssitem= new SRSSItem; // we create an item $rssitem->title = $item["title"]; // adding title (option) $rssitem->link = $item['link']; // adding link (option) $rssitem->pubDate = $item["date"]; // date automatically transformed into RSS format (option) $rssitem->description = $item["text"]; // adding description (mandatory) $rss->addItem($rssitem); // we add the item into our RSS }
There are 2 functions to add item.
The first one will add items in the order you enter them, from top to bottom.
$rss->addItem($item);
The other one does the opposite and add the next item in top of your RSS
$rss->addItemBefore($item);
<?php try{ header('Content-type: text/xml'); $q="SELECT id,title,body,UNIX_TIMESTAMP(pubDate) AS pubDate FROM articles LIMIT 0,15 ORDER BY pubDate DESC"; // last 15 articles $doGet=mysql_query($q); $rss = SRSS::create(); $rss->title = 'My Blog'; $rss->description = 'is the best'; $rss->link = 'http://shikiryu.com/devblog/'; while($result = mysql_fetch_array($doGet)){ $rssitem= new SRSSItem; $rssitem->title = $result['title']; // no need to strip anything $rssitem->link = 'http://shikiryu.com/devblog/article-'.$result['id']; $rssitem->pubDate = $result['pubDate']; // no need to format! $rssitem->description = $result['body']; $rss->addItem($rssitem); } echo $rss->show(); }catch(SRSSException $e){ header('HTTP/1.1 500 Internal Server Error'); }Then, when you call 'rss.php', the RSS will be displayed.
<?php try{ $rss = SRSS::read('http://shikiryu.com/devblog/rss.php'); ?> <h2><?=$rss->title;?> - <?=$rss->description?></h2><img src="<?=$rss->image()?>" alt="<?=$rss->image('title')?>" /> <ul> <?php foreach($rss as $article){ echo '<li><a href="'.$article->link.'">'. SRSSTools::formatDate('d/m/y', $article->pubDate).' '.$article->title.'</a></li>'; }?> </ul> <?php }catch(SRSSException $e){ echo 'Error while reading RSS : '.$e->getError(); }
You can contact me on my contact page or you can reach me on github