SitePoint Sponsor

User Tag List

Results 1 to 18 of 18

Thread: generating rss

  1. #1
    SitePoint Enthusiast crag's Avatar
    Join Date
    Apr 2003
    Location
    Ft. Lauderdale, FL
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    generating rss

    I've been given the task of creating a PHP program that will transform our database created pages to XML (for a rss feed). So i know the format of RSS so that's no problem. Any ideas where to start? I'm fairly new to writing PHP by hand so this is kind of my first official PHP task.

    I was thinking of creating the rss file with the proper heading and then using fwrite() (along with fopen() to open it in append mode) to write the XML line by line?. I figure this will work?

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think so yes
    - website

  3. #3
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have PHP 4.3+ and the DOMXML module enabled, try that; it's a lot better for generating XML.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or why not have the RSS as a template and then dynamically add your own data as when you need to ?

    There is a possiblity that you could even automate this process; so all the work is done for you and the finished template is stored to your server as a seperate file simply by a click of a button/cron ?

    I'm currently thinking about this method myself since the XML template/data insertion is already worked out using my classes

  5. #5
    SitePoint Enthusiast crag's Avatar
    Join Date
    Apr 2003
    Location
    Ft. Lauderdale, FL
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Or why not have the RSS as a template and then dynamically add your own data as when you need to ?
    That's what i want.... what's the difference between a template and a regular php doc?

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm....

    A template IMO is a file that holds the basic shell of a page and nothing more;

    No variables;
    No styles or formatting etc

    Just the shell and you then create your content that goes in there - most common method - by replacing specific tags in your template with your data.

    If you can wait a few days (busy at the moment) I'll post an XML template and some script for you to look at, or look at the PHP archives and/or my previous posts to see if you can find a post with my classes and sample templates ?

    There is at least 2 posts in there somewhere.

    Isn't all that difficult really - honest.

  7. #7
    SitePoint Enthusiast crag's Avatar
    Join Date
    Apr 2003
    Location
    Ft. Lauderdale, FL
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    ...most common method - by replacing specific tags in your template with your data.
    Okay i get this. but how do you find the tags? Do you have to search the template doc line by line - kind of like a search and replace?

    -crag

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That it basically - I use eregi though some folks say this is slow and use preg_match function instead; up to yourself really.

    When I've more time later today, I'll hunt down those threads I mentioned and post their URLs here...

  9. #9
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so that is how you parse your xml files, with regular expressions, no wonder people say they are slow...
    - website

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .... nope.

    No Reg. Exp. used at all in my classes.

    At some point, if I've not already done so, I'll proberly change over to preg_match() at some point anyway, although IMO there isn't that much of a speed gain by changing.


  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Crag - Here is the thread with my classes and some sample script to use them with an XML template.... also posted along with the above.

    http://www.sitepointforums.com/showt...5&pagenumber=1

    Remember though that the page number could be wrong - as I have my pages displayed in reverse order

    There is only 5 pages though so it'll not take you 2 minutes to find the page you need. Just scroll from top to bottom and you'll find it.

  12. #12
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what do you mean, you said you used 'eregi' which is a regular expressions function, if I am not very much mistaken ... ?
    - website

  13. #13
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    parsing XML carries an overhead , creating it is another matter ...
    you could just cheat and loop a SQL statement ... eg ..

    PHP Code:
    <?
    $sql 
    mysql_query("SELECT v_title as title, v_author as author ,v_body as content FROM v_news $where_params_order_etc");
    while( 
    $yaks=mysql_fetch_array$sql MYSQL_ASSOC ) ){
    $xml .= "\n<item>";
        foreach ( 
    $yaks as $k=>$v ){
            
    $xml .="\n<".$k.'>'.$v.'</'.$k.'>';
        }
    $xml .= '</item>';
    }

    echo 
    $rss_header $xml $rss_footer;
    ?>
    whether you save that or serve on request is irrelevant really , at least this way the data is always fresh from the DB

  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Website - look at the scripts ?

    In fact, looking myself I use ereg_replace()

    PHP Code:
    /** class method
      * replace a specified tag with dynamic content from a string
      */
      
    function AppendText2XmlFile($tag2parse$text) {
       
    $fp = @ file($this -> xmldir $this -> xmlfilename'r');
       
       if(
    $fp) {
        
    $xmltext '';
        while(list(
    $num$line) = each($fp)) {
         
    trim($line);
         
         
    $xmltext .= ereg_replace($tag2parse$text$line);
        }
       }
       
       
    # return xml document file to class as a string
       
    $this -> xmlfilename $xmltext;
      }
      
      
    /** class method
      * continue to replace a specific tag with dynamic content from a string
      */
      
    function AppendText2XmlString($tag2parse$text) {
       
    $this -> xmlfilename ereg_replace($tag2parse$text$this -> xmlfilename);
      } 
    Firepages....

    Yes, indeed you could use that method as well Though personally I will be sticking with my methods thank you...

    Btw there isn't that much of an overhead using Sablotron and XSL-T; a lot faster than SAX and DOMXML; from what I've read elsewhere of course, so I'm not suggesting that I have concrete benchmarks here...

    Again the parsed template doesn't have to be stored; it can be fed as and when requested as well I might add ?

  15. #15
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using regex to grab data from XML I can understand , using it to create it I cant

  16. #16
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm....

    Some folks.... I just don't know....


  17. #17
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dr Livingston, I was talking about when you are taking information from the xml file, not when inserting it to it, I might have used wrong word above...

    And firepages, I must second that
    - website

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    website - I was refering to firepages message

    ... I can understand , using it to create it I cant
    I insert data - generated from database for example - which is then transformed to xHTML via XSL-T.

    The XML is a file - template in other words; A simple concept that has many uses I might add ?



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
  •