Creating an RSS feed for Build-A-Blog entries

Home Forums Scripts GirlsWhoGeek Scripts Creating an RSS feed for Build-A-Blog entries

This topic contains 5 replies, has 3 voices, and was last updated by  echo 2 years, 11 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #15498

    SailorDonut
    Participant

    I’ve used Build-A-Blog on several of my sites, and I’m interested in creating an RSS feed so people can add my blog posts to their feed readers. I’ve been trying to figure out how to build it on my own, and I keep thinking I’ve got it, but then… it doesn’t work. Does anyone have any tips/links to tutorials/suggestions on how I might be able to do this? I’m hoping to make something that will pull the entries from the database automatically, without me having to manually update the XML file every time I post an entry. Thanks!

    #15499

    echo
    Participant

    OK, so I am shamefully beyond rusty when it comes to coding, but once upon a time I sort of knew what I was doing. 8 years ago I was looking to build an RSS feed as well, and after some frustrating failures, I managed to make it work. A great deal of the help I had came from Codegrrl.com forum conversations and sadly, they no longer exist. The rest came from other tutorial sites — maybe I Googled? — and I don’t remember them.

    However, we have my existing valid RSS feeds, so why don’t we give the code I cobbled together a shot!

    We’re going to use our friend PHP for this feed. Pick a name for your file (I went with rss.php), open PHP, then add some stuff to “please” the validators*, followed by your MySQL connection info & the code to display the number of entries (I went with 10):

    <?php
    
    // header("Content-Type: application/rss+xml; charset=iso-8859-1");
    // header("Content-Type: xml/text; charset=iso-8859-1");
    header("Content-Type: text/xml; charset=iso-8859-1"); // we chose "text/xml" to please the validators
    echo "<" . "?xml version=\"1.0\" encoding=\"iso-8859-1\" ?" . ">\n";
    
    mysql_connect ('localhost', 'db_username', 'db_password') ;
    mysql_select_db ('db_name');
    
    $sql = "SELECT * FROM php_blog ORDER BY id DESC LIMIT 10";
    $result = mysql_query($sql);
    
    ?>

    Now you’re going to get into the RSS channel and info, including “optional channel elements“:

    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
    <atom:link rel="self" type="application/rss+xml" href="http://yoursite.org/rss.php" />
    <title>Your Blog's Title</title>
    <link>http://yoursite.org/</link>
    <language>en-us</language>
    <description>A brief description of your site</description>
    <generator>I've named my script "echoLOG" so that's what I throw in here.</generator>
    <copyright>Copyright 2014 YOU!</copyright>
    <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>

    Time to open PHP again so you can grab info from your database!

    <?
    
    while ($row = mysql_fetch_array($result))
    {
    $id = $row["id"];
    $date = date("r",$row["timestamp"]);
    $title = $row["title"];
    $entry = $row["entry"];
    
    ?>
    

    Continue on with the RSS item info (I vaguely remember getting tripped up with the link display):

    <item>
    <author>youremail@yoursite.org (your name)</author>
    <title><?php echo "$title"; ?></title>
    <link>http://yoursite.org/journal.php?id=<?php echo "$id"; ?></link>
    <guid>http://yoursite.org/journal.php?id=<?php echo "$id"; ?></guid>
    <description><![CDATA[<?php echo "$entry"?>]]></description>
    <pubDate><?php echo "$date"; ?></pubDate>
    </item>
    

    We’re getting to the end, so we’d better close MySQL and your RSS channel!

    <?
    }
    mysql_close();
    ?>
    
    </channel>
    </rss>

    And that’s what the code for my feed looks like –> http://echoverse.org/rss.php
    Hopefully it will work. It’s 8 years old so perhaps there is an easier and/or better way to do it now; that I leave open to the more experienced, knowledgeable folks who post here.

    Let me know how it goes!

    *It’s always a good idea to “please” the W3C validators!
    **I don’t remember why <link> & <guid> are the same.

    #15500

    SailorDonut
    Participant

    *squeals* Thank you so much! This worked perfectly! I felt like I got a pretty good understanding of PHP and MySQL from the Build-A-Blog tutorial, so it was like… “I know what I *need* it to do, why can’t I figure out how to *make* it do what I want?” Your tutorial was so much more straightforward than the stuff I found on Google, and I totally understood every step, so I say Great Job! :-D Thank you so, so much, I really appreciate it.

    #15501

    echo
    Participant

    Thank you, I’m so happy to be of help!

    #15503

    Amelie
    Keymaster

    Thanks, echo – I’ll add this to the BAB tips article when I get a chance!

    #15505

    echo
    Participant

    Awesome, thanks!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.