Created
November 9, 2009 23:01
-
-
Save arian/230413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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