#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Creating RSS feed with php


    Hello;

    I am learning to create rss feed from mysql. I was wondering if this is the correct approach. Is there anythings I am missing? Thank you.
    PHP Code:
    <?php header("Content-Type: application/rss+xml; charset=ISO-8859-1");
    date_default_timezone_set('Australia/Brisbane');

    include 
    "secure_folder/config_database_class.php";
    $sql "SELECT * FROM ad_have ORDER BY id DESC LIMIT 500";    
    $number_of_rows DB::Load()->Execute($sql)->returnNumAffectedRows();

    $rssfeed '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>Share Accommodation</title>';
    $rssfeed .= '<link>http://www.flatmatescenter.com</link>';
    $rssfeed .= '<description>FlatmatesCenter.Com XML Feed</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright>Copyright (C) 2013 flatmatescenter.com</copyright>';
    $row=0;
    $results DB::Load()->Execute($sql)->returnArray(); 
    function 
    xml_entities($string) {
        return 
    strtr(
            
    $string
            array(
                
    "<" => "&lt;",
                
    ">" => "&gt;",
                
    '"' => "&quot;",
                
    "'" => "&apos;",
                
    "&" => "&amp;",
            )
        );
    }

    foreach(
    $results as $val=>$row) {
            
            
    $rssfeed .= '<item>';
            
    $rssfeed .= '<title>' xml_entities($row['title']) . '</title>';
            
    $rssfeed .= '<guid>http://flatmatescenter.com/flatmatescenter/ad_details/show_ad/'.$row['id'].'</guid>';
            
    $rssfeed .= '<link>' xml_entities($row['suburb']) . '</link>';
            
    $rssfeed .= '<description>' xml_entities($row['comments']) . '</description>';
            
    $rssfeed .= '<pubDate>' date("D, d M Y H:i:s O"strtotime($date)) . '</pubDate>';
            
    $rssfeed .= '</item>';
        }
     
        
    $rssfeed .= '</channel>';
        
    $rssfeed .= '</rss>';
     
        echo 
    $rssfeed;
    ?>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,986
    Rep Power
    9397
    1. I'd use htmlspecialchars() instead of xml_entities() and/or strtr(). Yes, it says HTML, but it works for XML too.
    2. Set isPermaLink="true" on the <guid>.

    Otherwise it looks fine. Does it validate?
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by requinix
    1. I'd use htmlspecialchars() instead of xml_entities() and/or strtr(). Yes, it says HTML, but it works for XML too.
    2. Set isPermaLink="true" on the <guid>.

    Otherwise it looks fine. Does it validate?
    Validate for what? Because there are no post or get value. The recorda re already in the database.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,986
    Rep Power
    9397
    Validate as RSS. More worthwhile for a larger feed - a small one is easy enough to eyeball.
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by requinix
    Validate as RSS. More worthwhile for a larger feed - a small one is easy enough to eyeball.
    Looks valid. But there are warnings.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,986
    Rep Power
    9397
    I'm sticking this in XML since that's probably going to be the main topic from here on.


    1. Don't use text/plain for an RSS feed (unless you're still testing it). Use application/xml
    2. You're double- and triple-encoding stuff. That's also the cause of the "no HTML in the title" warning.
    Code:
    <description>This magnificent architect designed pavilion home is set on two beautiful acres of lush green rain forest with high spacious balcony&amp;amp;#039;s to induge all your senses. Only 7 min drive from Byron central, spectacular beaches and popular restaurants. We also have a complimentary courtesy bus to byron central that runs twice daily.
    
    Holistic classes such a tai chi, yoga &amp;amp;amp; meditation as well as natural therapy treatments will be available at the retreat from the 1st of May.
    
    My house is your house, relax unwind, enjoy, smile and just be.
    
    AIR ROOM- This room is a twin occupancy room with two single beds. 
    RATE-$5o per room per person. 
    &amp;amp;amp;#65279; 
    &amp;amp;amp;#65279;WEEKLY SPECIAL, Book for 7 nights and pay only $325 per bed. 
    &amp;amp;amp;#65279;</description>
    3. The <pubDate> should be when the item appeared in the RSS feed, not some future "availability" date.
    4. The "missing atom:link" really is just a warning but it's easy to add so you might as well do it.
    5. The bad characters are probably the result of some encoding problem. They're neither ISO 8859-1 (as claimed in the XML) nor UTF-8 (the next likely possibility). What encoding are you using?

IMN logo majestic logo threadwatch logo seochat tools logo