Jump to content


Photo

Dynamically Updating/Creating RSS Feeds


  • Please log in to reply
5 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 27 April 2006 - 12:55 AM

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




#2 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 27 April 2006 - 01:16 AM

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.

<?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>


#3 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 27 April 2006 - 01:44 AM

Read the file into an array, write out all but the last 2lines, then add your content from the query. Then add the last to lines to close the code. I dont have time to explain anymore.

#4 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 27 April 2006 - 04:27 AM

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!

#5 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 27 April 2006 - 06:35 AM

-- SOLUTION ---

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

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

$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";

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.

<?
    /*
        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);
    }
?>


#6 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 27 April 2006 - 10:51 AM

I'll write one up when i get home, 8 hours.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users