PHP RSS Parser – RSS Reader Class for PHP
Just had to create a quick RSS parser for PHP and thought I'd post my solution. The implementation and class follows.
<?php
$rss = new RSSReader('http://news.google.com/?output=rss');
while ($rss -> hasNext())
print_r($rss -> next());
?>
<?php
class RSSReader {
var $xml = null;
var $pos = 0;
var $count = 0;
function __construct($feed_url) {
$this -> load_url($feed_url);
}
function load_url($feed_url) {
$this -> load_string(file_get_contents($feed_url));
}
function load_string($feed_string) {
$this -> xml = simplexml_load_string(str_replace('content:encoded', 'content_encoded', $feed_string));
$this -> pos = 0;
$this -> count = count($this -> xml -> channel -> item);
}
function get_title() {
return $this -> xml -> channel -> title;
}
function get_link() {
return $this -> xml -> channel -> link;
}
function get_pubdate() {
return $this -> xml -> channel -> pubdate;
}
function hasNext() {
return $this -> count > $this -> pos;
}
function next() {
$obj = $this -> xml -> channel -> item[$this -> pos++];
return array(
'title' => (string) $obj -> title,
'link' => (string) $obj -> link,
'description' => (string) $obj -> description,
'content' => (string) $obj -> content_encoded,
'pubDate' => strtotime($obj -> pubDate),
);
}
}
?>
Categories: Code
![[del.icio.us]](http://www.randomtools.net/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.randomtools.net/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://www.randomtools.net/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.randomtools.net/wp-content/plugins/bookmarkify/google.png)
![[Reddit]](http://www.randomtools.net/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://www.randomtools.net/wp-content/plugins/bookmarkify/slashdot.png)
![[StumbleUpon]](http://www.randomtools.net/wp-content/plugins/bookmarkify/stumbleupon.png)
Thank you for this post ..
Here is another php parser http://bncscripts.com/free-php-rss-parser/
RSS feeds are really great because you are always updated with the latest news or blog posts..”~