Wednesday, September 19, 2012

RSS Reader Class in PHP

9/19/2012

What is RSS?
RSS stands for Really Simple Syndication or Rich Site Summary. RSS is used by (among other things) news websites, weblogs and podcasting. RSS feeds provide web content, or summaries of web content, together with links to the full versions of the content. RSS delivers this information as an XML file called an RSS feed, webfeed, RSS stream, or RSS channel. In addition to facilitating syndication, RSS feeds allow a website's frequent readers to track updates on the site as soon as they become available using an aggregator. The aggregator provides a consolidated view of the content in a single browser display or desktop application. Such aggregators or applications are also referred to as RSS readers, feed readers, feed aggregators or news readers.

Universal RSS Reader Class:

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
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),
);
}
}?>
Usage:
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/
$rss = new RSSReader('http://news.google.com/?output=rss');
while ($rss -> hasNext())
print_r($rss -> next());
?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top