get RSS feed into php array - possible?

0 votes
asked Mar 16, 2010 by haroldo

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?


4 Answers

0 votes
answered Mar 16, 2010 by luca-filosofi

Short Version: ( NEW )


$feed = '';
$feed_to_array = (array) simplexml_load_file($feed);
//OR $feed_to_array = (array) new SimpleXmlElement( file_get_contents($feed) );


    [ShortName] => Stack Overflow
    [Description] => Search Stack Overflow: Q&A for professional and enthusiast programmers
    [InputEncoding] => UTF-8
    [Image] =>
    [Url] => SimpleXMLElement Object
            [@attributes] => Array
                    [type] => text/html
                    [method] => get
                    [template] =>{searchTerms}



Long Version: ( OLD )


$rss_tags = array(  
$rss_item_tag = 'item';  
$rss_url = '';

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

echo '<pre>';  

function rss_to_array($tag, $array, $url) {  
  $doc = new DOMdocument();  
  $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;  
0 votes
answered Mar 16, 2010 by tim-post

I believe Simplepie will do this for you as well.

0 votes
answered Mar 31, 2010 by daniel-iversen

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

0 votes
answered Mar 26, 2013 by desarrolla2

The best Feed consumer library in PHP is RSSClient[1]


Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter