├── demo └── index.php ├── README.md └── Source ├── Rss_Item.php └── Rss_Parser.php /demo/index.php: -------------------------------------------------------------------------------- 1 | load('http://feeds.feedburner.com/tweakers/mixed'); 8 | 9 | foreach($rss->getItems() as $item){ 10 | echo ''.$item->getTitle().'
'; 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rss_Parser 2 | ========== 3 | 4 | This is a simple PHP class to parse RSS feeds. 5 | 6 | Example 7 | ------- 8 | 9 | include_once '../Rss_Item.php'; 10 | include_once '../Rss_Parser.php'; 11 | 12 | $rss = new Rss_Parser(); 13 | $rss->load('http://feeds.feedburner.com/tweakers/mixed'); 14 | 15 | foreach($rss->getItems() as $item){ 16 | echo ''.$item->getTitle().'
'; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Source/Rss_Item.php: -------------------------------------------------------------------------------- 1 | title; 29 | } 30 | 31 | /** 32 | * 33 | * @param string $title 34 | * @return Rss_Item 35 | */ 36 | public function setTitle($title){ 37 | $this->title = $title; 38 | return $this; 39 | } 40 | 41 | /** 42 | * 43 | * @return string 44 | */ 45 | public function getLink(){ 46 | return $this->link; 47 | } 48 | 49 | /** 50 | * 51 | * @param string $link 52 | * @return Rss_Item 53 | */ 54 | public function setLink($link){ 55 | $this->link = $link; 56 | return $this; 57 | } 58 | 59 | /** 60 | * 61 | * @return string 62 | */ 63 | public function getDescription(){ 64 | return $this->description; 65 | } 66 | 67 | /** 68 | * 69 | * @param string $desc 70 | * @return Rss_Item 71 | */ 72 | public function setDescription($desc){ 73 | $this->description = $desc; 74 | return $this; 75 | } 76 | 77 | public function toArray(){ 78 | return array( 79 | 'title' => $this->getTitle(), 80 | 'link' => $this->getLink(), 81 | 'description' => $this->getDescription() 82 | ); 83 | } 84 | 85 | } -------------------------------------------------------------------------------- /Source/Rss_Parser.php: -------------------------------------------------------------------------------- 1 | 5 | * @license MIT-license 6 | */ 7 | 8 | class Rss_Parser { 9 | 10 | /** 11 | * 12 | * @var DOMDocument 13 | */ 14 | protected $doc; 15 | 16 | /** 17 | * 18 | * @var array 19 | */ 20 | protected $items = array(); 21 | 22 | public function __construct(){ 23 | $this->doc = new DOMDocument(); 24 | } 25 | 26 | /** 27 | * 28 | * @param string $url 29 | * @return Rss_Parser 30 | */ 31 | public function load($url){ 32 | $this->doc->load($url); 33 | return $this; 34 | } 35 | 36 | /** 37 | * 38 | * @return DOMNode 39 | */ 40 | protected function getChannel(){ 41 | return $this->doc->getElementsByTagName('channel')->item(0); 42 | } 43 | 44 | /** 45 | * 46 | * @param $reparse Should it reparse the rss feed if it was already done 47 | * @return array with instances of Rss_Item 48 | */ 49 | public function getItems($reparse=false){ 50 | if(empty($this->items) || $reparse){ 51 | $channel = $this->getChannel(); 52 | 53 | foreach($channel->getElementsByTagName("item") as $domItem){ 54 | $this->items[] = $this->parseItem($domItem); 55 | } 56 | } 57 | return $this->items; 58 | } 59 | 60 | /** 61 | * Get a certain item from the items 62 | * @param $i 63 | * @return Rss_Item 64 | */ 65 | public function getItem($i){ 66 | $items = $this->getItems(); 67 | if(isset($items[$i])){ 68 | return $items[$i]; 69 | } 70 | return false; 71 | } 72 | 73 | /** 74 | * Parses an item 75 | * @param DOMNode $item 76 | * @return Rss_Item 77 | */ 78 | protected function parseItem(DOMNode $item){ 79 | $rss_item = new Rss_Item(); 80 | $rss_item->setTitle( $item->getElementsByTagName("title")->item(0)->firstChild->data) 81 | ->setLink( $item->getElementsByTagName("link")->item(0)->firstChild->data) 82 | ->setDescription( $item->getElementsByTagName("description")->item(0)->firstChild->data); 83 | 84 | return $rss_item; 85 | } 86 | } 87 | --------------------------------------------------------------------------------