php 将 RSS 提要放入 MySQL 数据库的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/471103/
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
What is the best way to get RSS Feeds into a MySQL Database
提问by Tyler Carter
I am trying to take several RSS feeds, and put the content of them into a MySQL Database using PHP. After I store this content, I will display on my own page, and also combine the content into one single RSS Feed. (Probably after filtering)
我正在尝试获取几个 RSS 提要,并使用 PHP 将它们的内容放入 MySQL 数据库中。存储此内容后,我将显示在我自己的页面上,并将这些内容合并为一个 RSS Feed。(大概是过滤后)
I haven't dealt with RSS Feeds before, so I am wondering the best Framework/Method of doing this is. I have read about DOM based parsing, but have heard that it takes a lot of memory, any suggestions?
我以前没有处理过 RSS 提要,所以我想知道这样做的最佳框架/方法是什么。我读过基于 DOM 的解析,但听说它需要大量内存,有什么建议吗?
采纳答案by Paul Dixon
Magpieis a reasonable RSS parser for PHP. Easy to use:
Magpie是一个合理的 PHP RSS 解析器。便于使用:
require('rss_fetch.inc');
$rss = fetch_rss($url);
An item like this for example:
例如这样的项目:
<item rdf:about="http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257">
<title>Weekly Peace Vigil</title>
<link>http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257</link>
<description>Wear a white ribbon</description>
<dc:subject>Peace</dc:subject>
<ev:startdate>2002-06-01T11:00:00</ev:startdate>
<ev:location>Northampton, MA</ev:location>
<ev:enddate>2002-06-01T12:00:00</ev:enddate>
<ev:type>Protest</ev:type>
</item>
Would be turned into an array like this:
会变成这样的数组:
array(
title => 'Weekly Peace Vigil',
link => 'http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257',
description => 'Wear a white ribbon',
dc => array (
subject => 'Peace'
),
ev => array (
startdate => '2002-06-01T11:00:00',
enddate => '2002-06-01T12:00:00',
type => 'Protest',
location => 'Northampton, MA'
)
);
Then you can just pick out the bits you want to save in the DB and away you go!
然后,您可以选择要保存在数据库中的位,然后就可以了!
回答by Trystian Sky
The Best PHP parser out there is SimplePie, IMHO. I've been using it for years. It's great at grabbing and parsing the following: RSS 0.90, RSS 0.91 (Netscape), RSS 0.91 (Userland), RSS 0.92, RSS 1.0, RSS 2.0, Atom 0.3, Atom 1.0; including the following namespaces: Dublin Core 1.0, Dublin Core 1.1, GeoRSS, iTunes RSS 1.0 (mostly complete), Media RSS 1.1.1, RSS 1.0 Content Module, W3C WGS84 Basic Geo, XML 1.0, XHTML 1.0
最好的 PHP 解析器是SimplePie,恕我直言。我已经使用它多年了。它非常擅长抓取和解析以下内容:RSS 0.90、RSS 0.91 (Netscape)、RSS 0.91 (Userland)、RSS 0.92、RSS 1.0、RSS 2.0、Atom 0.3、Atom 1.0;包括以下命名空间:Dublin Core 1.0、Dublin Core 1.1、GeoRSS、iTunes RSS 1.0(大部分完成)、Media RSS 1.1.1、RSS 1.0 Content Module、W3C WGS84 Basic Geo、XML 1.0、XHTML 1.0
SimplePie 1.2 even has database caching, so it should have everything you need to do what you want.
SimplePie 1.2 甚至有数据库缓存,所以它应该有你做你想做的一切。
And if you need to parse raw XML files, try using XMLize
如果您需要解析原始 XML 文件,请尝试使用 XMLize
-Trystian
-特里斯蒂安
回答by Daniel Iversen
For a very simple hacked together script that just works end-to-end (parse RSS, insert into DB);
对于一个非常简单的脚本,它只是端到端的工作(解析 RSS,插入到数据库中);
回答by acrosman
There are several RSS parsing libraries out there, including Magpieand one in pear.
I would pick a parser, and then run it through a loop with the data to feed it into the database. Make sure you figure out how often you want to run the script, and think about if this is running from cron, or part of a page that is only loaded infrequently.
我会选择一个解析器,然后通过循环运行它,将数据输入到数据库中。确保您弄清楚您想要运行脚本的频率,并考虑它是从 cron 运行的,还是只加载很少的页面的一部分。

