SћíкïяўЦЯSS

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.

Why use it?

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! :)

How to use it?

First of all, you should document yourself with RSS 2.0 specifications. You can find them here.

A quick overview :

Global informations
  • title : your rss title (mandatory)
  • link : your website url (from where the RSS comes) (mandatory)
  • description : your rss description / baseline (mandatory)
  • language : used in your RSS (optionnal)
  • copyright : your copyright (optionnal)
  • managingEditor : email of blog editor (optionnal)
  • webMaster : webmaster email (optionnal)
  • pubDate : date where content was added in channel (optionnal)
  • lastBuildDate : last date change (optionnal)
  • category : article's category (optionnal)
  • generator : what generated your rss ? I hope it's SRSS :) (optionnal)
  • docs : links to RSS specifications (optionnal)
  • cloud : can register with a cloud (optionnal)
  • ttl : time to be cached in minutes (optionnal)
  • image : your logo (optionnal)
  • rating : the PICS rating (optionnal)
  • textInput : useless... (not implemented)
  • skipHours : hours aggregators can skip (optionnal)
  • skipDays : days aggregators can skip (optionnal)
Item informations
  • title : post's title (optionnal)
  • link : post's url (optionnal)
  • description : post's description (mandatory)
  • author : post's author (optionnal)
  • category : post's category (optionnal)
  • comments : post's comments url (optionnal)
  • enclosure : post's media description (optionnal)
  • guid : string identifying the post (optionnal)
  • pubDate : publication date of the post (optionnal)
  • source : -- (not yet implemented)

You'll need PHP5 at least

How to make it read RSS?

First, we need to load the RSS :
$rss = SRSS::read('http://exemple.com/rss.xml');
Easy, right? Then you can extract general informations :
echo $rss->title; // will display blog title
Then, 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

How to make it create RSS?

First, we need to initialize the RSS :
$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.
Then, you can add articles. Let's imagine $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);

Old school example for RSS creation (rss.php):

<?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.

Example to display RSS

<?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();
}

FAQ

What if RSS file can't be reach?
In case of file reading, the RSS file may not be downloaded properly (server down, invalid file, etc.). No error will be displayed by ShikiryuRSS but you can have a fallback while surrounding your reading code with a try/catch and trying to catch a SRSSException. See our example.
What can I do with my new RSS created file?
You can make an array with it with toArray().
You can display it with show()
You can save it with save($path).

Contact

  • Need more info?
  • Need new methods?
  • Need to ask something?
  • Need to say hi?

You can contact me on my contact page or you can reach me on github