• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with RSS + PHP
#1
For a long time, I struggled to get RSS to play nicely with PHP. After much research and revising PHP, I finally got some script that works.

The first thing to realise is that RSS is just XML, if you'd like to learn more about XML head over to http://www.w3schools.com
I will walk you through how my code works.
PHP Code:
<?php

//BLOG
$xml=("http://thoughtspeech.wordpress.com/feed/");
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace false//ignore useless childNodes which are just blank space 
$xmlDoc->load($xml);
?>
$xml should equal your RSS feed. Mine points to my ThoughtSpeech blog.
The rest basically loads the feed into a variable that we can now edit. So we carry on.

PHP Code:
<?php

//BLOG
$xml=("http://thoughtspeech.wordpress.com/feed/");
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace false//ignore useless childNodes which are just blank space 
$xmlDoc->load($xml);


$value 0;
$post $xmlDoc->getElementsByTagName"item" ); 
foreach( 
$post as $item 

    
$Titles $item->getElementsByTagName"title" ); 
    
$Title $Titles->item(0)->nodeValue
  
    
$Descs $item->getElementsByTagName"description" ); 
    
$Desc $Descs->item(0)->nodeValue
    
    
$Links $item->getElementsByTagName"link" ); 
    
$Link $Links->item(0)->nodeValue;
    
    
$Title_Array[$value] = $Title;
    
$Description_Array[$value] = $Desc;
    
$Link_Array[$value] = $Link;
    
    
$value++;
    


Lets start with
PHP Code:
$post $xmlDoc->getElementsByTagName"item" ); 
This assigns $post with an array with all of the "items" or posts from your Blog.
Now lets examine part of the foreach loop, from which you can work out the rest of it. The rest of the foreach that I show you will examine the Title tag, but it can be applied to the others with a few small changes.
PHP Code:
$Titles $item->getElementsByTagName"title" ); 
    
$Title $Titles->item(0)->nodeValue
This examines the $post/$item variable for all the elements with the name "title", it then assigns it too $Title.
PHP Code:
$Title_Array[$value] = $Title;
$value++; 
This part of code is completely optional, it creates an array `creatively` named $Title_Array, with all of the titles in it.

Note: When getting tags, please note that they are case-sensitive.

Until Next Time

-Drumm
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply
#2

That would most probably (will) take longer than using preg_match().
  Reply
#3
I chose this method because it allows me slightly more control, I haven't had any problems. It isn't intended for any extremely heavy use, and It could easily be optimised. I don't pretend to be a PHP Expert, if you PM me any changes you see fit, I will happily incorporate them + give credit where due.
Also, quoting a huge post like that can be a pain, I know it isn't against forum rules, but it isn't exactly.. Nice ? To go through all that crap first.
It's much appreciated.

-Drumm
  Reply


Forum Jump: