RSS feed on your site in PHP

Discussion in 'PHP' started by mariush, Feb 14, 2006.

  1. #1
    I've just written a RSS feed generator to place on my site.

    Here's in a few easy steps what you have to do to have your own little orange icon in Firefox:

    * Create a file called rss.php ( or name it whatever you wish but remember the name)

    * Type the following code in it (or copy and paste):

    
    <?
    header("Content-Type: application/xml");
    
    $site_title="My Site";  // type here your feed/site name
    $site_description="The name of the syte"; // type here the feed/site description
    $site_url="http://www.google.com"; // type here the site address
    
    print "<rss version=\"0.91\">\n";
    print "<channel>\n";
    print "<title>".$site_title."</title>\n";
    print "<description>".$site_description."</description>\n";
    print "<link>".$site_url."</link>\n";
    
    PHP:
    For every item that you wish to add in the RSS feed, you have to repeat the following code:

    
    $title = "item title"; // type here the title of the rss item
    $description = "item description"; // type here a description of the rss item
    $link = "http://www.google.com"; //type here the address where user can read more about it
    	
    print "<item>\n";
    print " <title>".$title."</title>\n";
    print " <description>".$description."</description>\n";
    print " <link>".$link."</link>\n";
    print "</item>\n";
    
    PHP:
    For example, a possible ideea is to take the title, description and link of the news articles on your site from a database and, in a while instruction, to use the code mentioned above.

    The last piece of code closes the RSS feed:

    
    print "</channel>\n</rss>\n";
    
    ?>
    
    PHP:
    Note: Make sure there are absolutely NO characters before the php start tag "<?" in the file, otherwise there will be errors !

    Now that we have the rss feed, we need to tell browsers that there is a feed on our site.

    Open your index page and add the following line after the <head> html tag (or between the head tags as shown below):

    
    <head>
    <link rel="alternate" type="application/rss+xml" href="http://www.YourSite.com/rss.php" title="The Name Of The Feed">
    </head>
    
    HTML:
    Replace "http:// www. YourSite .com /rss.php" with the full address of your site pointing to the file rss.php and change the name in the title attribute to the name of your feed.

    That's it :)
     
    mariush, Feb 14, 2006 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Its amazing how many tutorials on this seem to miss out the header Content-Type: application/xml - well done for putting it in. This had me confused the first time I did a feed.
     
    mad4, Feb 14, 2006 IP
  3. MamboCube

    MamboCube Peon

    Messages:
    242
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    cool, been looking for this exact thing to put my articles in rss.

    Looks alot simpler than I thought too.

    Thanks mariush!

    This site seems to have everything!!!!
     
    MamboCube, Feb 19, 2006 IP