Advertisement
nateshoffner

BnS News Feed Parser

May 11th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. class FeedItem
  3. {
  4.     private $url;
  5.     private $title;
  6.     private $author;
  7.     private $published;
  8.  
  9.     public function __construct($url, $title, $author, $published)
  10.     {
  11.         $this->url          = $url;
  12.         $this->title        = $title;
  13.         $this->author       = $author;
  14.         $this->published    = $published;
  15.     }
  16.  
  17.     public function getURL()
  18.     {
  19.         return $this->url;
  20.     }
  21.  
  22.     public function getTitle()
  23.     {
  24.         return $this->title;
  25.     }
  26.  
  27.     public function getAuthor()
  28.     {
  29.         return $this->author;
  30.     }
  31.  
  32.     public function getPublished()
  33.     {
  34.         return $this->published;
  35.     }
  36. }
  37.  
  38. class NewsFeed implements Iterator
  39. {
  40.     private $position = 0;
  41.     private $feeditems = array();
  42.  
  43.     public function __construct()
  44.     {
  45.         $this->Update();
  46.     }
  47.  
  48.     public function Update()
  49.     {
  50.         if (count($this->feeditems) > 0)
  51.         {
  52.             unset($this->feeditems);
  53.             $this->feeditems = array();
  54.         }
  55.  
  56.         $feed_xml = simplexml_load_file('http://buildandshoot.com/feed.php?mode=news');
  57.  
  58.         foreach ($feed_xml->entry as $entry)
  59.         {
  60.             $url        = $entry->link['href'];
  61.             $title      = $entry->title;
  62.             $author     = $entry->author->name;
  63.             $published  = $entry->published;
  64.  
  65.             array_push($this->feeditems, new FeedItem($url, $title, $author, $published));
  66.         }
  67.     }
  68.  
  69.     function rewind()
  70.     {
  71.         $this->position = 0;
  72.     }
  73.  
  74.     function current()
  75.     {
  76.         return $this->feeditems[$this->position];
  77.     }
  78.  
  79.     function key()
  80.     {
  81.         return $this->position;
  82.     }
  83.  
  84.     function next()
  85.     {
  86.         ++$this->position;
  87.     }
  88.  
  89.     function valid()
  90.     {
  91.         return isset($this->feeditems[$this->position]);
  92.     }
  93. }
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement