获取RSS feed进入php数组 – 可能吗?

我想从另一个网站用php解析一个现有的RSS提要,然后把它的某些部分存储在一个mysql数据库中.

我非常适合php和mysql,但从来没有使用过rss feed,我应该从哪里开始?

>有没有相当于file_get_contents()让rss进入php?
是将rss feed分解成xml / microdata还是需要使用正则表达式来获取位?

干杯!

简短版本:(新)

演示:http://so.devilmaycode.it/get-rss-feed-into-php-array-possible/

$feed = 'http://stackoverflow.com/opensearch.xml';
$feed_to_array = (array) simplexml_load_file($feed);
//OR $feed_to_array = (array) new SimpleXmlElement( file_get_contents($feed) );
print_r($feed_to_array);

//output

Array
(
    [ShortName] => Stack Overflow
    [Description] => Search Stack Overflow: Q&A for professional and enthusiast programmers
    [InputEncoding] => UTF-8
    [Image] => http://sstatic.net/stackoverflow/img/favicon.ico
    [Url] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [type] => text/html
                    [method] => get
                    [template] => http://stackoverflow.com/search?q={searchTerms}
                )

        )

)

长版本:(OLD)

<?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;  
}  
?>
相关文章
相关标签/搜索