Jump to content

Dynamically Updating/Creating RSS Feeds


JustinK101

Recommended Posts

Hello All,

Sorry for my lack of knowledge about RSS/XML but I suppose thats why I am here.

I currently run a classifieds for college students (http://www.mycollegepost.com). Everything is created in html/php and then stored in mySQL. I want, basically everytime a new classified is added to the database, to put the title/url of the classified in an rss feed. Then users can subscribe to the feed and see new classified ads easily and dynamically, and click the title link and go to the corresponding ad.

So I assume I need to write to a rss/xml file dynamically when I do my insert code into the database.

Anyone with experience I would greatly appreciate help. I really have no idea how to setup the rss feed either. Thanks.

-- Justin


Link to comment
Share on other sites

Ok here is the template I want to use:

Note, I ran this through an rss feed validator and apparently it does not like urls with variables passed by get example CollegeName= and folder=. This is going to be a problem, because I must pass variables in the links.

[code]
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>

<title>MyCollegePost.com :: USC :: RSS Feed</title>
<description>MyCollegePost.com bringing traditional on-campus bulletin board online. A simple and straightforward method for students to do anything from selling or buying an item, finding a party, requesting a tutor, locating housing, or even finding a job. MyCollegePost brings buyers and sellers together in a free market, where they can exchange goods and services with people in their own college community.</description>
<link>http://www.mycollegepost.com/USC/index.php?collegeName=University+of+Southern+California&folder=USC</link>

<item>
<title>Classified Title Here</title>
<description>First 50 charcaters of message body here.</description>
<link>Link to post here.</link>
</item>

// Next item, and so on. These items need to generated dynamically though. //

</channel>
</rss>
[/code]
Link to comment
Share on other sites

Rab,

Thanks for your comments.

Can anyone give some code example how to read from the existing rss file and then know where to insert a new a ITEM and then write a new ITEM. Should new items be appended into the first position or the last position in the rss file?

Assume I have:

$title = "TITLE HERE"
$link = "URL OF POST"
$desc = "FIRST 50 Characters Of The Message"

Thanks!
Link to comment
Share on other sites

[b]-- SOLUTION ---[/b]

[b]The following are two functions I wrote, took a bit, that solve my problem. Hopefully somebody else will find them VERY useful as well.[/b]

Here is a sort example of the use of the two functions:

[code]
$rssTitle = "Some Title Here";
$rssDescription = "Some message/description here";
$rssLink = "http://www.somelinkhere.com";

// YOU MUSE USE THIS FORMAT. THIS IS THE XML/RSS DATE STANDARD FORMAT
$rssPubDate = date('D, d M Y H:i:s T');

$rssLanguage = "en-us";
[/code]

[b]NOTE: EVERYWHERE YOU SEE 'USE-%-HERE' remove that and just call the function name itself. FOR SOME REASON I HAD TO CHANGE THE FUNCTION NAMES TO EVEN POST THIS MESSAGE IN THE FORUM. I WAS GETTING ACCESS DENIED ERRORS LEAVING THESE FUNCTION NAMES ORGINAL AND TRYING TO POST. IT WAS REALLY PISSING ME OFF.[/b]

[code]
<?
    /*
        Function takes five parameters title, description,
        link, pubDate, and language and returns a string
        representing a new item in rss format. Note: The
        description is trimmed to 200 characters maximum.
    */
    function format_new_item($title, $description, $link, $pubDate, $language)
    {
        $stripChars = array("\n", "\r", "\t");
        $description = str_replace($stripChars, " ", $description);
        
        if(strlen($description) > 200)
        {
            $description = substr($description, 0, 197) . "...";
        }

        $title = "<title>" . $title . "</title>\n";
        $description = "<description>" . substr($description, 0, 200) . "</description>\n";
        $link = "<link>" . $link . "</link>\n";
        $pubDate = "<pubDate>" . $pubDate . "</pubDate>\n";
        $language = "<language>" . $language . "</language>\n";
        
        return "\n<item>\n" . $title . $description . $link . $pubDate . $language . "</item>\n";
    }
    
    function write_item_to_rss($rssFileName, $item)
    {
        if (!is_writable($rssFileName))
            die("Error: Cannot write to specified RSS file. The file may not exist or the file permissions may be set incorrectly.");
        
        /*
            Open rssFileName and set the file pointer to the beginning
            of the xml document. Allowed to read and write the xml text
            document.
        */
        $filePtr = use-fopen-here($rssFileName, "r+t");
        
        /*
            Seek the pointer to the end of the xml document.
            Then, seek the pointer back 18 characters from
            the end of the xml document. You may be asking
            why do this? It is because of the length of the
            closing rss and channel tags.
            
            </channel>\n
            </rss>
        */    
        use-fseek-here($filePtr, -18, SEEK_END);

        /*
            Append the closing channel and rss tags into item.
        */
        $item = $item . "\n</channel>\n</rss>";
        
        /*
            Write item to rssFileName at postion pointer.
        */
        use-fwrite-here($filePtr, $item);
            
        /*
            Close the pointer.
        */
        use-fclose-here($filePtr);
    }
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.