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

简短版本:(新)

$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)

$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 '

';

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;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值