A Digital Age Deserves A Digital Leader

Parsing RSS feeds with PHP

Parsing RSS feeds with PHP

Postby gries818 » Mon Oct 13, 2008 8:07 pm

Is there a simpler way to parse RSS feeds with PHP? Currently I'm using this to parse FOXnews' Political RSS feed:
http://techrepublic.com.com/html/tr/sid ... 834-2.html

I want to end up using PHP to print out a link like this:

Code: Select all
<a href="http://www.foxnews.com/story/0,2933,436781,00.html">Bush Welcomes Italian Prime Minister to White House</a>


Thanks!
Image

Mac OS 10.6.7 - Personal
Ubuntu Server 11.04 - Server
Software Development
User avatar
Posts: 3991
Joined: Wed Jul 07, 2004 6:28 pm

Postby imnuts » Tue Oct 14, 2008 4:20 am

I have a parser around somewhere, let me see if I can find it. I had one that would display some news and a link to the rest of it and another that would just print out the links. Let me see if I can find them for you.

EDIT: Here is one and the source. It has problems with apostrophe's in subject titles though, and there are a couple other bugs I think as well, but it's basically what you're looking for a think. http://www.imnuts.org/test/
Image
PRO SUPREME
User avatar
Posts: 7457
Joined: Wed Mar 24, 2004 5:19 am
Location: Boothwyn, Pennsylvania
Real Name: Mark

Postby gries818 » Tue Oct 14, 2008 9:39 pm

imnuts wrote:I have a parser around somewhere, let me see if I can find it. I had one that would display some news and a link to the rest of it and another that would just print out the links. Let me see if I can find them for you.

EDIT: Here is one and the source. It has problems with apostrophe's in subject titles though, and there are a couple other bugs I think as well, but it's basically what you're looking for a think. http://www.imnuts.org/test/


Hey that's great! Thanks!

I've got both the FOXNews Politics and the CNN Politics Feeds running through the script (obviously in separate files) and it seems to be working just fine.
Image

Mac OS 10.6.7 - Personal
Ubuntu Server 11.04 - Server
Software Development
User avatar
Posts: 3991
Joined: Wed Jul 07, 2004 6:28 pm

Postby imnuts » Wed Oct 15, 2008 8:02 am

It tends to work well at times. I tried 3-4 different RSS feeds in it and all still linked properly, just had some issues when non-alphanumeric characters were in the title, but not always. I also got a couple errors about invalid XML tags, though I could never find anything wrong, so I don't know if it was a one time thing or what.
Image
PRO SUPREME
User avatar
Posts: 7457
Joined: Wed Mar 24, 2004 5:19 am
Location: Boothwyn, Pennsylvania
Real Name: Mark

Re: Parsing RSS feeds with PHP

Postby gries818 » Sat Mar 14, 2009 6:33 pm

I revisited this again today because I need to import a most recent Twitter update for a project I was working on. Since I asked for help parsing feeds with PHP, I've gained a lot more experience using PHP and so I completely rewrote the script using OOP.

Code: Select all
<?
class RSS
{
   protected $rss_url;
   protected $rss;
   protected $item;
   protected $array_current;
   protected $result;
   public $final;
   
   protected function assignRSS($requested_url)
   {
      if ($requested_url != "") {
         $this->rss_url = $requested_url;
      }
      $this->rss = simplexml_load_file($this->rss_url);
      $this->item = $this->rss->channel->item;
   }
   
   protected function loop_through()
   {
      $this->final = "<ul>\n";      
      $this->array_current = 0;
      foreach ($this->item as $this->result) {
         $this->final = $this->final . "<li><a href=\"" . $this->result->link . "\" target=\"_blank\">" . $this->result->title . "</a></li>\n";         
         $this->array_current = $this->array_current + 1;
      }
      $this->final = $this->final . "</ul>\n";
   }
   
   public function getRSS($requested_url)
   {
      $this->assignRSS($requested_url);
      $this->loop_through();
      
      return $this->final;
   }
}

class Twitter extends RSS
{
   //Declaration of needed variables
   protected $uname_length;   
   private $statuses;
   private $while_substr;
   
   protected function assign_twitter($requested_id, $requested_uname)
   {
      $this->rss_url = "http://twitter.com/statuses/user_timeline/" . $requested_id . ".rss";
      $this->uname_length = strlen($requested_uname . ": ");
      $this->assignRSS("");
   }
   
   protected function assign_array_variables()
   {
      $this->array_current = 0;
      foreach ($this->item as $this->result) {
         $this->statuses[$this->array_current] = substr_replace($this->result->title, "", 0, $this->uname_length);
         $this->array_current = $this->array_current + 1;
      }
   }
   
   protected function loop_mostRecent()
   {
      $this->array_current = 0;
      do {
         $this->while_substr = substr($this->statuses[$this->array_current], 0, 1);
         
         if ($this->while_substr != "@") {
            $this->final = $this->statuses[$this->array_current] . "\n";
            $this->array_current = "";            
         }
         elseif ($this->while_substr == "@") {
            $this->array_current = $this->array_current + 1;   
         }
         elseif ($this->while_substr == "") {
            $this->final = "No recent status updates" . "\n";
            $this->array_current = "";
         }
      }   
      while ($this->statuses[$this->array_current] != "");
   }
   
   public function getStatus($requested_id, $requested_uname)
   {
      $this->assign_Twitter($requested_id, $requested_uname);            
      $this->assign_array_variables();      
      $this->loop_mostRecent();
      
      return $this->final;
   }
}
?>


For the RSS feed, use this code to display it in the file you want:

Code: Select all
<?
include("class_RSS_functions.php");
$newsfeed = new RSS();
echo $newsfeed->getRSS("http://feeds.foxnews.com/foxnews/politics.rss");
?>


And for Twitter, use this code:

Code: Select all
<?
include("class_RSS_functions.php");
$twitter = new Twitter();
echo $twitter->getStatus("user_id", "username");
?>


I'm not claiming this script is better... though it works just fine for what I use it for and since I wrote it, I understand it better :lol:
Image

Mac OS 10.6.7 - Personal
Ubuntu Server 11.04 - Server
Software Development
User avatar
Posts: 3991
Joined: Wed Jul 07, 2004 6:28 pm

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 2 guests