tags:

views:

246

answers:

5

I want to parse an existing RSS feed from another website with php and then store certain parts of it in a mysql database.

I'm very competent with php and mysql but have never worked with rss feeds before, where should i start?

  1. is there an equivalent to file_get_contents() for getting rss into php?
  2. are rss feeds broken down into xml/microdata or do i need to use regex to grab bits?

cheers!

+1  A: 

You want Zend Feed, it will do everything you want and more :)

Andy
i don't know if i really want to start using a framework, is there not just an rss parsing class out there somewhere?
Haroldo
you can use the class by itself, Zend is a loosely coupled framework, no dependencies!
Andy
+5  A: 

Try This:

<?php

$rss_tags = array(  
'title',  
'link',  
'guid',  
'comments',  
'description',  
'pubDate',  
'category',  
);  
$rss_item_tag = 'item';  
$rss_url = 'http://www.webaddict.info/feeds/news.xml';

$rssfeed = rss_to_array($rss_item_tag, $rss_tags, $rss_url);

echo '<pre>';  
print_r($rssfeed);

function rss_to_array($tag, $array, $url) {  
  $doc = new DOMdocument();  
  $doc->load($url);  
  $rss_array = array();  
  $items = array();  
  foreach($doc-> getElementsByTagName($tag) AS $node) {  
    foreach($array AS $key => $value) {  
      $items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;  
    }  
    array_push($rss_array, $items);  
  }  
  return $rss_array;  
}  
?>
aSeptik
cool, i'll give it a try.I also found this: http://magpierss.sourceforge.net/ ?
Haroldo
+1 for suggesting DOM parsing for RSS analyzation instead of using a library, although there were many different formats out there the last time I have written my own RSS reader.
Residuum
Woah, weird quotes you had there. I fixed it for you.
Franz
sounds good bro! ;-)
aSeptik
this is perfect, works a treat and using just 1 function!(the example doesn't work because the rss feed no longer exists)thanks again
Haroldo
Also make sure you checkout SimpleXML which is fantastic for dealing with any XML, including RSS. http://uk.php.net/simplexml
DavidYell
A: 

I believe Simplepie will do this for you as well.

Tim Post
A: 

If others come past, an end-to-end very simple free code example of this is on;

http://code.google.com/p/rssingest/

Daniel Iversen
A: 

The first thing that springs to mind is SimpleXML - Have a look at http://www.ibm.com/developerworks/library/x-simplexml.html.

However, aSeptik's answer looks much easier, and is probably more efficient... so maybe you should ignore this!

Joseph Redfern