MaxStocker.com   MaxStocker.com    
   
Home About Blog Stuff Contact
 
   
 

May 2009

The other day...
Posted : Thu May 28th

Added Java to Category List
Posted : Tue May 26th

Proper exception handling can't wait
Posted : Mon May 25th

Connection woes
Posted : Sat May 23rd

Why I hate Apple
Posted : Wed May 20th

Extracting DOCX content with Java
Posted : Tue May 19th

I notice
Posted : Fri May 15th

JDBC Best Practices
Posted : Fri May 15th

Simple PHP RSS loader
Posted : Sun May 10th

MySQL and DBCP for Tomcat 5
Posted : Sat May 9th

Tracing 316.70.50.1
Posted : Mon May 4th

The flip side
Posted : Tue April 28th

Starting to irritate me
Posted : Fri April 24th

As seen on the internet
Posted : Wed April 22nd

Recent Comments

Max in Whose blog is it anyway?
on Mon May 10th

Rob in Whose blog is it anyway?
on Fri May 7th

Anonymous in SEO and the magic beans
on Thu April 8th

Max in SEO and the magic beans
on Thu April 8th

n.o. in SEO and the magic beans
on Thu April 8th

silky in Right way, wrong way
on Fri February 19th

Categories

Technical
68 Entries

Security
18 Entries

Java
23 Entries

Privacy
6 Entries

Database
11 Entries

Internet
56 Entries

Business
31 Entries

Site Updates
19 Entries

Personal
85 Entries

RSS Feed RSS Feed

Tag Cloud

Simple PHP RSS loader
Posted : Sunday May 10th, 2009

A little while ago I wrote a simple RSS parser and loader in PHP for a client. The purpose for me was to read an RSS feed and copy the contents all so the clients blogspot blog can also be presented on their own website.

Anyway here is the main guts of the script, it uses cURL and XML/DOM.

<?php
$ch = curl_init();
$feedurl = "http://yourfeedurlhere";
curl_setopt($ch, CURLOPT_URL, $feedurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$blogfeed = curl_exec($ch);
curl_close($ch);
$blog = DOMDocument::loadXML($blogfeed);
$entries = $blog->getElementsByTagName("item");
foreach($entries as $entry){
  $pubdatenode = $entry->getElementsByTagName("pubDate");
  $pubdate = $pubdatenode -> item(0)->nodeValue;
  $adate = date("Y-m-d H:i:s",strtotime($pubdate));
  $titlenode = $entry->getElementsByTagName("title");
  $title = $titlenode -> item(0)->nodeValue;
  $descriptionnode = $entry->getElementsByTagName("description");
  $description = $descriptionnode -> item(0)->nodeValue;
}
?>

So inside the foreach loop there are three variables parsed out of each item in the feed. One for the title (title), the posting date (adate) and the description or content field (description). I then take that date and optionally update a database with the information but you could do whatever you need to at that point. I haven't extracted the link or comment fields because I either already know that information or don't even really want it, again you could extract that as well if you wished, the same applies for other channel entities.

At any rate there are other, more complex libraries, out there that will turn the feed into a list of objects for you but for my needs a simple solution works well so it might help someone else as well.

Tags

code  parsing  PHP  reading  RSS 

Categories

Technical  Internet 

Comments

 
   
  Follow me on Twitter   My Facebook Profile   My LinkedIn Profile   RSS feed of my blog Home   |   About   |   Blog   |   Stuff   |   Contact   |   Privacy Policy  
   
  © 2008 Max Stocker