Wrath
Wrath

Reputation:

RSS feeds in PHP

Just wondering if someone could suggest a PHP library that would allow me to read the data of an RSS feed and write it to a MySQL database. Also, if possible, provide a link to documentation about how to do this?

Thanks

Upvotes: 5

Views: 1462

Answers (6)

Phyxx
Phyxx

Reputation: 16075

SimplePie is definitely the way to go. You can download and parse an RSS feed in under 10 lines of code. There is a tutorial here that shows you how.

Upvotes: 1

Kyle J. Dye
Kyle J. Dye

Reputation: 1724

You can also give XPath a try, pretty simple to use.

Upvotes: 0

gnud
gnud

Reputation: 78528

RSS is a pretty simple format - there is no great need to use a separate library.

I'd just use simplexml, because I don't wanna spend the effort learning another library, and keeping up with its development.

Here is a simple PHP script for showing the latest Stackoverflor posts with simplexml:

<?php
$rss = simplexml_load_file('http://stackoverflow.com/feeds');
?>
<h1><?php echo $rss->title; ?></h1>
<ul>
<?php
foreach($rss->entry as $e) {
    echo "<li><a href=\"".$e->link['href']."\">";
    echo $e->title; 
    echo "</a></li>\n";
}    
?>
</ul>

Upvotes: 10

Cups
Cups

Reputation: 6896

Reading data that simple is something you could use simplexml for.

http://www.ibm.com/developerworks/library/x-simplexml.html

From there you can see how easy it is to grab the data, instead of displaying, you store in a database.

Gotchas: badly formatted rss (see above tut) different flavours of rss (ditto) evil values in the rss - it's foreign data and should be handled with suspicion dependency on allow_url_fopen - maybe use cURL

Upvotes: 2

Steve Claridge
Steve Claridge

Reputation: 11080

Magpie is an excellent RSS library

http://magpierss.sourceforge.net/

There's an example of using it here: http://www.olate.co.uk/articles/view.php?id=214

Once you've used Magpie to grab an RSS feed you can store it in a string a save to a DB as you would with any other string.

Upvotes: 0

Daff
Daff

Reputation: 44205

Simplepie is probably the most popular PHP RSS library.

Upvotes: 3

Related Questions