1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php script to create RSS-feed

Discussion in 'PHP' started by sgthayes, Oct 9, 2005.

  1. #1
    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 :
    
    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;
    	}
    };
    
    PHP:
    You would use it like this :
    SEMrush
    
    $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();
    
    PHP:
    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.
     
    sgthayes, Oct 9, 2005 IP
    olddocks likes this.
    SEMrush
  2. netaddict

    netaddict Peon

    Messages:
    640
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It looks impressive :) Can we have a live example using this script?
     
    netaddict, Oct 10, 2005 IP
  3. Dread

    Dread Peon

    Messages:
    323
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'll certinly give it a go.
     
    Dread, Oct 10, 2005 IP
  4. Connect

    Connect Guest

    Messages:
    191
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is cool. Have been looking for something like this. Thanks for sharing.
     
    Connect, Oct 11, 2005 IP
  5. sgthayes

    sgthayes Peon

    Messages:
    171
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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)
     
    sgthayes, Oct 18, 2005 IP
  6. webdesigners123

    webdesigners123 Peon

    Messages:
    282
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    we will try it also ! very impressive !!!
     
    webdesigners123, Oct 26, 2005 IP
  7. Shoemoney

    Shoemoney $

    Messages:
    4,475
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #7
    I highly recomend magpie rss. It has built in cacheing and other neato tools
     
    Shoemoney, Oct 26, 2005 IP
  8. webdesigners123

    webdesigners123 Peon

    Messages:
    282
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    where it can be found ?
     
    webdesigners123, Oct 26, 2005 IP
  9. sgthayes

    sgthayes Peon

    Messages:
    171
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    sgthayes, Oct 26, 2005 IP
  10. Shoemoney

    Shoemoney $

    Messages:
    4,475
    Likes Received:
    588
    Best Answers:
    0
    Trophy Points:
    295
    #10
    ahhhhh sorry maybe magpie is not the solution here... my bad
     
    Shoemoney, Oct 26, 2005 IP
  11. webdesigners123

    webdesigners123 Peon

    Messages:
    282
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you for the link
     
    webdesigners123, Oct 26, 2005 IP
  12. onedollar

    onedollar SEO Consultant for Hire

    Messages:
    3,481
    Likes Received:
    333
    Best Answers:
    0
    Trophy Points:
    0
    #12
    sgthayes,

    how does a static website create a rss feed from your code above ?
     
    onedollar, Oct 26, 2005 IP
  13. smo

    smo Peon

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I think you have to add titles , description etc manually in the second part of the script.
     
    smo, Oct 28, 2005 IP
  14. sgthayes

    sgthayes Peon

    Messages:
    171
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.
     
    sgthayes, Oct 28, 2005 IP
  15. smo

    smo Peon

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    smo, Oct 29, 2005 IP
  16. DPTony

    DPTony Peon

    Messages:
    329
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Looks nice my friend.
     
    DPTony, Oct 29, 2005 IP
  17. bingbong

    bingbong Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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
     
    bingbong, Nov 7, 2006 IP
  18. kks_krishna

    kks_krishna Active Member

    Messages:
    1,495
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    80
    #18
    How to add author name in each item? I have created rss file using your script. Its realy good. But its not showing the author name. How can i set the author name.
     
    kks_krishna, Jun 23, 2007 IP
  19. Brandon Sheley

    Brandon Sheley Illustrious Member

    Messages:
    9,713
    Likes Received:
    609
    Best Answers:
    2
    Trophy Points:
    420
    #19
    nice script

    I'll check this out on a spare url that I'm testing on.

    cheers
     
    Brandon Sheley, Jun 23, 2007 IP
  20. anton-io!

    anton-io! Active Member

    Messages:
    540
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #20
    Great script and after all this time (since 2005) this thing works great
    Outputing to file and place cron on .php file
    Thanks! :D
     
    anton-io!, Feb 15, 2008 IP