将 RSS 提要放入 php 数组 - 可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2454979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:33:38  来源:igfitidea点击:

get RSS feed into php array - possible?

phprss

提问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.

我想用 php 解析来自另一个网站的现有 RSS 提要,然后将其中的某些部分存储在 mysql 数据库中。

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

我非常擅长 php 和 mysql,但以前从未使用过 rss 提要,我应该从哪里开始?

  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?
  1. 是否有等效于 file_get_contents() 的用于将 rss 导入 php?
  2. RSS 提要是否分解为 xml/microdata 或者我是否需要使用正则表达式来获取位?

cheers!

干杯!

回答by Luca Filosofi

Short Version:( NEW )

简短版本:(新)

demo:http://so.lucafilosofi.com/get-rss-feed-into-php-array-possible/

演示:http : //so.lucafilosofi.com/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}
                )

        )

)


Long Version:( 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;  
}  
?>

回答by Tim Post

I believe Simplepiewill do this for you as well.

我相信Simplepie也会为您做到这一点。

回答by Daniel Iversen

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

如果其他人过去了,一个端到端的非常简单的免费代码示例正在开启;

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

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

回答by desarrolla2

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

PHP 中最好的 Feed 消费者库是 RSSClient[1]

[1] https://github.com/desarrolla2/RSSClient

[1] https://github.com/desarrolla2/RSSClient