SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Publishing RSS feed with PHP

    Sorry if there are already posts covering this or it is a really basic question, but I'm trying to take values from a tabbed data plain text file and syndicate a RSS feed from them.
    I want to make a php script to run on the server, updating a RSS feed periodically from the file.
    Is php a good way to do this? Is there some other easily learnable techology that would do it better? I already know some python...
    Cheers for your help...

  2. #2
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm already assuming you understand the RSS format, if not, hop onto google and start reading PHP is a fine way to do this but in the end it really doesn't matter.

    Anyway, all you do is write a PHP script to automatically read the plain text file and then spit out the RSS equivalent. Make the PHP script available online and simply point an RSS reader to the script file (http://www.foo.org/rss.php, for example).
    This space for rent.

  3. #3
    SitePoint Enthusiast earthlingzed's Avatar
    Join Date
    Jul 2004
    Location
    athens, ga
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a really great RSS feed genration class at http://www.bitfolge.de/rsscreator-en.html that is easy to use. There is a forum too if you have questions.

  4. #4
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That class looks good. Should do the job of the creation of the feed nicely.
    The only problem I have now is the problem of making the feed update itself.
    The script I am trying to write will take values from a webpage format them into XML and publish them as a feed.
    I just can't quite wrap my head around the event that will cause the feed to update itself?
    Should the php be in the actual feed, so that every request for the feed results in the script being run and the feed updated?
    Or should the php be in a separate place, and when it is called the feed is updated.
    I believe the webpage that the values are coming from will only be updated a few tims a day, if that...

  5. #5
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by half_brick
    I just can't quite wrap my head around the event that will cause the feed to update itself?
    Let's say I have a file "rss.php" which is available to the general web at "http://www.agoossens.org/rss.php". In rss.php I generate the RSS XML and print it to the client.

    The reader will make a request to the address http://www.agoossens.org/rss.php when it synchronises for the first time, since that's where I told it to find the feed. That will obviously cause rss.php to be executed and eventually spit out the required XML. Any changes you make to the text file will be sent to the user on the very next request to the PHP script.

    The PHP script *is* the feed, in the sense that it generates it. Each request to the script file will cause the feed to be re-created. Does that clear it up for you? Or am I misinterpreting your question? Curse the Internet for not conveying subtleties!
    This space for rent.

  6. #6
    SitePoint Enthusiast earthlingzed's Avatar
    Join Date
    Jul 2004
    Location
    athens, ga
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I created an RSS builder script, and when I call the method in the class to finish the RSS build, I set the RSS display parameter to false so it just builds and stores the file
    PHP Code:
    echo $rss->saveFeed("RSS2.0""/public_html/rss/feed.xml",false); 
    . Then in my content management sytem where you add, edit and delete records, I just did
    PHP Code:
    require_once('rssbuilder.php'); 
    as the last item of business after manipulating the record. That way anytime you do something with your content, the RSS will reflect your changes.

    PHP Code:
    <?
    // RSS 2.0 Feed Creator

    require_once("/public_html/cl/feedcreator.class.php"); 
    require_once(
    "/public_html/cl/MySQL.php"); 
    require_once(
    "/public_html/t/constants.php"); 

    // Format Time
    function iso8601_date($time) {
        
    $tzd date('O',$time);
        
    $tzd $tzd[0] . str_pad((int) ($tzd 100), 2"0"STR_PAD_LEFT) .
        
    ':' str_pad((int) ($tzd 100), 2"0"STR_PAD_LEFT);
        
    $date date('Y-m-d\TH:i:s'$time) . $tzd;
        return 
    $date;
    }



    // Generate Feed
    $rss = new UniversalFeedCreator(); 
    $rss->useCached(); // use cached version if age<1 hour
    $rss->title "Some Cool Feed"
    $rss->description "Some good description"

    //optional
    $rss->descriptionTruncSize 5000;
    $rss->descriptionHtmlSyndicated true;

    $rss->link "http://somesite.com/rss/feed.xml"
    $rss->syndicationURL "http://somesite.com/rss/feed.xml"

    $image = new FeedImage(); 
    $image->title "somesite.com logo"
    $image->url "http://somesite.com/i/logo.gif"
    $image->link "http://somesite.com/teaching/"
    $image->description "Feed provided by somesite.com. Click to visit."

    //optional
    $image->descriptionHtmlSyndicated true;
    $rss->image $image


    // Instantiate DB Object
    $db = &new MySQL(DBHOST,DBUSER,DBPASS,DBNAME);



    // Feed Build //////////////////////////////////////////////////////////////////////////////////////////////////////

    $sql "SELECT * FROM news ORDER BY dateadded";
    $files $db->query($sql);

    while( 
    $row $files->fetch() ){
        
        
    $item = new FeedItem(); 
        
    $item->title $row['title']; 
        
    $item->link "http://somesite.com/storage/" urlencode($row['filename']);
        
    $item->description $row['description']; 
        
        
    //optional
        
    $item->descriptionTruncSize 500;
        
    $item->descriptionHtmlSyndicated false;

        
    $item->date iso8601_date($row['dateadded']);  
        
    $item->source "http://www.somesite.com/"
        
    $item->author $row['author'] . ' nospam@example.com'
        
        
    // Enclosure
        
    $item->enclosure = new EnclosureItem();
        
    $item->enclosure->url='http://somesite.com/storage/' urlencode($row['filename']);
        
    $item->enclosure->length=$row['filesize'];
        
    $item->enclosure->type=$row['filetype'];
         
        
    $rss->addItem($item); 



    // valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
    // MBOX, OPML, ATOM, ATOM0.3, HTML, JS
    echo $rss->saveFeed("RSS2.0""/public_html/rss/feed.xml",false);
    ?>
    Note: I ran into some trouble with the date displaying correctly. I was storing the data/time in the DB as an INT, and found I had to use that crazy function at the top to correctly transform into a date the RSS class would work with.
    Also, I got an updated version of the FeedCreator class so I could generate an RSS 2.0 feed (uses the enclosure tag to attach files). I got it here
    http://mypapit.net/drop/files/feedcreator-1.7.2-ppt.zip

  7. #7
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks. I've had a little play and I think I can see how it's gonna work. The overview of the project is that that the feed will be accessed by a system that controls public infomation screens. But as of yet I don't know exactly how the system wants the feeds formatted and whether it needs a feed with a specific name or extension.
    The feed itself is updated from a publicly available webpage with tabbed UV values on it. So anyupdating of that webpage is nothing to do with us and we cannot automatically trigger the feed to update at that stage.
    I think I've got the grand scheme of it all worked tho, now I just need some firm requirements from the customer.
    Cheers for your help guys.

  8. #8
    SitePoint Zealot dustbuster's Avatar
    Join Date
    Jan 2003
    Location
    End-World
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earthlingzed
    Note: I ran into some trouble with the date displaying correctly. I was storing the data/time in the DB as an INT, and found I had to use that crazy function at the top to correctly transform into a date the RSS class would work with.
    Also, I got an updated version of the FeedCreator class so I could generate an RSS 2.0 feed (uses the enclosure tag to attach files). I got it here
    http://mypapit.net/drop/files/feedcreator-1.7.2-ppt.zip
    Would this function still be needed if the date was store as a DATETIME or TIMESTAMP?

    Is this new class from the same creator?

    Thanks
    "If all else fails brute force is always an option."
    M.E.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •