Skip to content

Instantly share code, notes, and snippets.

@arian
Created November 9, 2009 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arian/230413 to your computer and use it in GitHub Desktop.
Save arian/230413 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Arian Stolwijk <http://www.aryweb.nl>
* @license MIT-license
*/
/*
$rss = new Rss_Parser();
$rss->load('http://feeds.feedburner.com/tweakers/mixed');
foreach($rss->getItems() as $item){
echo '<a href="'.$item->getLink().'">'.$item->getTitle().'</a><br />';
}
*/
class Rss_Parser {
/**
*
* @var DOMDocument
*/
protected $doc;
/**
*
* @var array
*/
protected $items = array();
public function __construct(){
$this->doc = new DOMDocument();
}
/**
*
* @param string $url
* @return Rss_Parser
*/
public function load($url){
$this->doc->load($url);
return $this;
}
/**
*
* @return DOMNode
*/
protected function getChannel(){
return $this->doc->getElementsByTagName('channel')->item(0);
}
/**
*
* @param $reparse Should it reparse the rss feed if it was already done
* @return array with instances of Rss_Item
*/
public function getItems($reparse=false){
if(empty($this->items) || $reparse){
$channel = $this->getChannel();
foreach($channel->getElementsByTagName("item") as $domItem){
$this->items[] = $this->parseItem($domItem);
}
}
return $this->items;
}
/**
* Get a certain item from the items
* @param $i
* @return Rss_Item
*/
public function getItem($i){
$items = $this->getItems();
if(isset($items[$i])){
return $items[$i];
}
return false;
}
/**
* Parses an item
* @param DOMNode $item
* @return Rss_Item
*/
protected function parseItem(DOMNode $item){
$i = new Rss_Item();
$i->setTitle( $item->getElementsByTagName("title")->item(0)->firstChild->data)
->setLink( $item->getElementsByTagName("link")->item(0)->firstChild->data)
->setDescription( $item->getElementsByTagName("description")->item(0)->firstChild->data);
return $i;
}
}
class Rss_Item {
/**
*
* @var string
*/
protected $title;
/**
*
* @var string
*/
protected $link;
/**
*
* @var string
*/
protected $description;
/**
*
* @return string
*/
public function getTitle(){
return $this->title;
}
/**
*
* @param string $title
* @return Rss_Item
*/
public function setTitle($title){
$this->title = $title;
return $this;
}
/**
*
* @return string
*/
public function getLink(){
return $this->link;
}
/**
*
* @param string $link
* @return Rss_Item
*/
public function setLink($link){
$this->link = $link;
return $this;
}
/**
*
* @return string
*/
public function getDescription(){
return $this->description;
}
/**
*
* @param string $desc
* @return Rss_Item
*/
public function setDescription($desc){
$this->description = $desc;
return $this;
}
public function toArray(){
return array(
'title' => $this->getTitle(),
'link' => $this->getLink(),
'description' => $this->getDescription()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment