Home > Code > PHP RSS Parser – RSS Reader Class for PHP

PHP RSS Parser – RSS Reader Class for PHP

February 22nd, 2010 Leave a comment Go to comments

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),
		);
	}
}
?>
Bookmark this page
[del.icio.us] [Digg] [Facebook] [Google] [Reddit] [Slashdot] [StumbleUpon]
Categories: Code Tags:
  1. free rss parser
    July 9th, 2010 at 17:59 | #1

    Thank you for this post ..
    Here is another php parser http://bncscripts.com/free-php-rss-parser/

  2. July 28th, 2010 at 04:27 | #2

    RSS feeds are really great because you are always updated with the latest news or blog posts..”~

  1. No trackbacks yet.