Search Engine Forums
Psychics
This is an ad revenue sharing forumPut Your Advertisment Here

Go Back   Digital Point Forums > Design & Development > Programming > PHP
Register FAQ/Rules Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
  #1  
Old Oct 9th 2005, 2:56 pm
sgthayes sgthayes is offline
Berserker
 
Join Date: Sep 2005
Location: Belgium
Posts: 172
sgthayes will become famous soon enough
php script to create RSS-feed

I use this piece of code to create the rss-feeds on my sites. I want to share this with you all as I have learned a lot from these boards and want to give something back.

the code :
php Code:
class RSSFeed {
// VARIABLES
    // channel vars
    var $channel_url;
    var $channel_title;
    var $channel_description;
    var $channel_lang;
    var $channel_copyright;
    var $channel_date;
    var $channel_creator;
    var $channel_subject;   
    // image
    var $image_url;
    // items
    var $items = array();
    var $nritems;
   
// FUNCTIONS
    // constructor
    function RSSFeed() {
         $this->nritems=0;
        $this->channel_url='';
        $this->channel_title='';
        $this->channel_description='';
        $this->channel_lang='';
        $this->channel_copyright='';
        $this->channel_date='';
        $this->channel_creator='';
        $this->channel_subject='';
        $this->image_url='';
    }   
    // set channel vars
    function SetChannel($url, $title, $description, $lang, $copyright, $creator, $subject) {
        $this->channel_url=$url;
        $this->channel_title=$title;
        $this->channel_description=$description;
        $this->channel_lang=$lang;
        $this->channel_copyright=$copyright;
        $this->channel_date=date("Y-m-d").'T'.date("H:i:s").'+01:00';
        $this->channel_creator=$creator;
        $this->channel_subject=$subject;
    }
    // set image
    function SetImage($url) {
        $this->image_url=$url
    }
    // set item
    function SetItem($url, $title, $description) {
        $this->items[$this->nritems]['url']=$url;
        $this->items[$this->nritems]['title']=$title;
        $this->items[$this->nritems]['description']=$description;
        $this->nritems++;   
    }
    // output feed
    function Output() {
        $output'<?xml version="1.0" encoding="iso-8859-1"?>'."\n";
        $output .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">'."\n";
        $output .= '<channel rdf:about="'.$this->channel_url.'">'."\n";
        $output .= '<title>'.$this->channel_title.'</title>'."\n";
        $output .= '<link>'.$this->channel_url.'</link>'."\n";
        $output .= '<description>'.$this->channel_description.'</description>'."\n";
        $output .= '<dc:language>'.$this->channel_lang.'</dc:language>'."\n";
        $output .= '<dc:rights>'.$this->channel_copyright.'</dc:rights>'."\n";
        $output .= '<dc:date>'.$this->channel_date.'</dc:date>'."\n";
        $output .= '<dc:creator>'.$this->channel_creator.'</dc:creator>'."\n";
        $output .= '<dc:subject>'.$this->channel_subject.'</dc:subject>'."\n";

        $output .= '<items>'."\n";
        $output .= '<rdf:Seq>';
        for($k=0; $k<$this->nritems; $k++) {
            $output .= '<rdf:li rdf:resource="'.$this->items[$k]['url'].'"/>'."\n"
        };   
        $output .= '</rdf:Seq>'."\n";
        $output .= '</items>'."\n";
        $output .= '<image rdf:resource="'.$this->image_url.'"/>'."\n";
        $output .= '</channel>'."\n";
        for($k=0; $k<$this->nritems; $k++) {
            $output .= '<item rdf:about="'.$this->items[$k]['url'].'">'."\n";
            $output .= '<title>'.$this->items[$k]['title'].'</title>'."\n";
            $output .= '<link>'.$this->items[$k]['url'].'</link>'."\n";
            $output .= '<description>'.$this->items[$k]['description'].'</description>'."\n";
            $output .= '<feedburner:origLink>'.$this->items[$k]['url'].'</feedburner:origLink>'."\n";
            $output .= '</item>'."\n"
        };
        $output .= '</rdf:RDF>'."\n";
        return $output;
    }
};
You would use it like this :

php Code:
$myfeed = new RSSFeed();
$myfeed->SetChannel('http://www.mysite.com/xml.rss',
          'My feed name',
                  'My feed description',
          'en-us',
          'My copyright text',
                  'me',
          'my subject');
$myfeed->SetImage('http://www.mysite.com/mylogo.jpg');
$myfeed->SetItem('http://www.mysite.com/article.php?id=bla',
                'name',
                'description');
....
echo $myfeed->output();
I have a cron script that runs every now and then that writes the output to a an xml file. I ran it through a feed validator and it validates.

I hope this might be useful to somebody. Do with it what you will.

Last edited by digitalpoint : Oct 9th 2005 at 3:17 pm. Reason: Added [php] BBCodes
Reply With Quote
  #2  
Old Oct 10th 2005, 12:51 pm
netaddict netaddict is offline
Tauren
 
Join Date: Apr 2005
Location: Internet
Posts: 632
netaddict has a spectacular aura aboutnetaddict has a spectacular aura about
Send a message via MSN to netaddict Send a message via Yahoo to netaddict
It looks impressive Can we have a live example using this script?
__________________
Analyze Backlinks Quickly - Stop using the yahoo siteexplorer tool !

WTB: Links and blog posts related to health, fitness and beauty industry
Reply With Quote
  #3  
Old Oct 10th 2005, 5:51 pm
Dread's Avatar
Dread Dread is offline
Raider
 
Join Date: Sep 2005
Location: Edinburgh, Scotland
Posts: 323
Dread is on a distinguished road
I'll certinly give it a go.
Reply With Quote
  #4  
Old Oct 11th 2005, 4:22 am
Connect's Avatar
Connect Connect is offline
Berserker
 
Join Date: Aug 2005
Posts: 228
Connect is on a distinguished road
This is cool. Have been looking for something like this. Thanks for sharing.
Reply With Quote
  #5  
Old Oct 18th 2005, 2:11 pm
sgthayes sgthayes is offline
Berserker
 
Join Date: Sep 2005
Location: Belgium
Posts: 172
sgthayes will become famous soon enough
Quote:
Can we have a live example using this script?
My cooking site uses this script to generate it's feed. Have a look at http://www.zucchini.be/rss.xml (it's in dutch)
Reply With Quote
  #6  
Old Oct 26th 2005, 2:43 am
webdesigners123's Avatar
webdesigners123 webdesigners123 is offline
Raider
 
Join Date: Oct 2005
Posts: 282
webdesigners123 is on a distinguished road
we will try it also ! very impressive !!!
__________________
Free Business Cards - Just get it for FREE...
Looking for Web Designers ? Try our FREE Service
Reply With Quote
  #7  
Old Oct 26th 2005, 2:44 am
Shoemoney's Avatar
Shoemoney Shoemoney is offline
Banned
 
Join Date: Sep 2004
Location: Nebraska, USA
Posts: 4,542
Shoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond repute
Send a message via AIM to Shoemoney Send a message via Skype™ to Shoemoney
I highly recomend magpie rss. It has built in cacheing and other neato tools
Reply With Quote
  #8  
Old Oct 26th 2005, 2:57 am
webdesigners123's Avatar
webdesigners123 webdesigners123 is offline
Raider
 
Join Date: Oct 2005
Posts: 282
webdesigners123 is on a distinguished road
where it can be found ?
__________________
Free Business Cards - Just get it for FREE...
Looking for Web Designers ? Try our FREE Service
Reply With Quote
  #9  
Old Oct 26th 2005, 3:07 am
sgthayes sgthayes is offline
Berserker
 
Join Date: Sep 2005
Location: Belgium
Posts: 172
sgthayes will become famous soon enough
Magpie can be found at http://magpierss.sourceforge.net/
It is used to parse an rss-feed. It does not create rss feeds.
I use it on my news site. It is indeed very good.
Reply With Quote
  #10  
Old Oct 26th 2005, 3:27 am
Shoemoney's Avatar
Shoemoney Shoemoney is offline
Banned
 
Join Date: Sep 2004
Location: Nebraska, USA
Posts: 4,542
Shoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond reputeShoemoney has a reputation beyond repute
Send a message via AIM to Shoemoney Send a message via Skype™ to Shoemoney
ahhhhh sorry maybe magpie is not the solution here... my bad
Reply With Quote
  #11  
Old Oct 26th 2005, 3:27 am
webdesigners123's Avatar
webdesigners123 webdesigners123 is offline
Raider
 
Join Date: Oct 2005
Posts: 282
webdesigners123 is on a distinguished road
Thank you for the link
__________________
Free Business Cards - Just get it for FREE...
Looking for Web Designers ? Try our FREE Service
Reply With Quote
  #12  
Old Oct 26th 2005, 3:43 am
onedollar's Avatar
onedollar onedollar is offline
SEO Consultant for Hire
 
Join Date: Sep 2005
Location: www.HostingCoupons.org
Posts: 3,607
onedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant futureonedollar has a brilliant future
sgthayes,

how does a static website create a rss feed from your code above ?
Reply With Quote
  #13  
Old Oct 28th 2005, 5:06 am
smo smo is offline
Grunt
 
Join Date: Sep 2005
Posts: 38
smo is on a distinguished road
I think you have to add titles , description etc manually in the second part of the script.
Reply With Quote
  #14  
Old Oct 28th 2005, 8:09 am
sgthayes sgthayes is offline
Berserker
 
Join Date: Sep 2005
Location: Belgium
Posts: 172
sgthayes will become famous soon enough
Yep, if your site is static you'll have to add the items manually OR you could make a script that parses the directory where your static files are, then open each file and use the <title> tag for instance as the title of the feeditem and add them this way. Depends on how much static files there are and how frequently you create new ones.
Reply With Quote
  #15  
Old Oct 29th 2005, 6:04 am
smo smo is offline
Grunt
 
Join Date: Sep 2005
Posts: 38
smo is on a distinguished road
I use mysql table and add title, url , desc and date by using a form. So when ever I add content to my site I use that form and store these info in the table. Then another page creates the rss.xml page out of this table info by taking the last 5 new entries, it also creates another JavaScript feed with the records.

As I don't add more than 4 or 5 pages in a week so easy for me to handle this way.
Reply With Quote
  #16  
Old Oct 29th 2005, 8:02 am
DPTony DPTony is offline
Raider
 
Join Date: Oct 2005
Location: Maine
Posts: 334
DPTony is an unknown quantity at this point
Looks nice my friend.
Reply With Quote
  #17  
Old Nov 7th 2006, 1:23 pm
bingbong bingbong is offline
Peon
 
Join Date: Nov 2006
Posts: 2
bingbong is on a distinguished road
can you give us the files please?

hi, I'm not taht good with php yet, can you post these files to download please? can how did you write the corn job? it will be very very helpful!

thanks
Reply With Quote
<