www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: rss feed

  1. #16
    Join Date
    May 2011
    Posts
    58
    Quote Originally Posted by criterion9 View Post
    Usually I point to a PHP file directly rather than to get the engine to process all xml files. ie: rss.php instead of rss.xml.
    hmm but how you use .php for rss? i mean from php how you make .xml for feed?

  2. #17
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    NO -- Use php to create the rss.xml file, by writing it to the server, as I mentioned earlier. It is a simple process.

    Just name your current file rss_generator.php and put all the xml statement into a the header or content. This is what I have done... may need some debugging. This is some old code, and I took out some of it for simplicity.

    PHP Code:
    include_once ('connect2db.php'); // connect to the database

    $xmlFile = 'rss.xml';
    // Generating RSS from PHP and MySQL Database.
    $rss_header = "<?xml version='1.0' ?>\n"; 
    $rss_header .= "<rss version='2.0'>\n";
    $rss_header .= "<channel>\n";
    $rss_header .= "<title>My Test Title</title>\n";
    $rss_header .= "<image>\n";
    $rss_header .= "<title>My Test Title</title>\n";
    $rss_header .= "<link>http://www.mywebsite.com</link>\n";
    $rss_header .= "<url>http://www.mywebsite.com/images/logosmall.jpg</url>\n";
    $rss_header .= "</image>\n";
    $rss_header .= "<link>http://www.mywebsite.com</link>\n";
    $rss_header .= "<description>My description</description>\n";
    $rss_header .= "<language>en-us</language>\n";
    $rss_header .= "<copyright>Copyright 2011, Me</copyright>\n";

    $rss_footer = "</channel>\n</rss>"; // end of rss 


    //Query to retrive data to update RSS and Twitter with.

    $query = "SELECT Title, Author, Category, Introtext,Publish_Up, URLS, DATE_FORMAT(Publish_Up, '%a, %d %b %Y %T') AS MyDate, DATE_FORMAT(Publish_Up, '%Y-%m-%d %H:%i:%s') AS MyDate2 FROM content WHERE Priority > 0 AND Publish_Dwn > DATE_ADD(NOW(), INTERVAL 1 HOUR) ORDER BY ID DESC LIMIT 20";

    //Format for Date_Format: "Fri, 28 Jul 2006 13:00 MST"

    $result = mysql_query($query,$link);
    $n = 0;
    $rssItem ="";

    date_default_timezone_set('America/Chicago');
    $fileDate = date ("Y-m-d H:i:s", filemtime($xmlFile));
    clearstatcache();


    while ($a_row = mysql_fetch_array($result, MYSQL_ASSOC)){
        
        // test for a new update (compare database date-time to rss.xml date-time)
        if ($n == 0){
            $newestDate = $a_row['MyDate2'];
        //echo "New: ".$newestDate." File time: ".$fileDate."<br><br>";
            }
        // if date of file is older open the file for re-writing (open only once)
        if ($n == 0 && $newestDate > $fileDate){         
    /* *********************************************************
    */
    // continue with RSS feed update
        $rssCategory = $a_row['Category'];
        $rssDescription = substr(strip_tags(html_entity_decode($a_row['Introtext'])),0,200);
        $rssDescription = addslashes(htmlspecialchars_decode($rssDescription,ENT_QUOTES));
            if ($rssCategory == 1){
                $rssItem = "<item>\n<title>". $a_row['Title']. "</title>\n<description>\n\n <![CDATA[ ".$rssDescription."[...] by ".$a_row['Author']." ]]>\n\n </description><link>" . $a_row['URLS'] . "</link>\n<guid>". $a_row['URLS']."</guid>\n<pubDate>" . $a_row['MyDate'] . " MST</pubDate>\n</item>\n\n";
            }    
            if($rssCategory == 4 || $rssCategory == 5){
                $rssItem = "<item>\n<title>Event or Alert</title>\n <description>\n\n <![CDATA[ " .$rssDescription. "... ]]>\n\n </description><link> http://www.mnsd56.com</link>\n<guid>http://www.mnsd56.com/#".$n."</guid>\n<pubDate>" . $a_row['MyDate'] . " CDT</pubDate>\n</item>\n\n";
            }
            
            $rssContent =    $rssContent . $rssItem;
            $rssDebug = $rssDebug ."<br> N= ". $n." ".$rssDescription;    //keep adding more rssItems    
            $n++;
            
        } // End of While Loop

    // Completed the while loop, now write footer, close the RSS file and done. 
    if ($debug == 0 && $newestDate > $fileDate){
        $fp = fopen($xmlFile, "w"); // make a new RSS file each time. 
        fwrite($fp, $rss_header);  
        fwrite($fp,$rssContent);
        fwrite($fp, $rss_footer);
        fclose($fp);
        }

    // That's all folks
    If you start fiddling with running xml files as php files, I assure you, you are looking for a lot of trouble.

  3. #18
    Join Date
    May 2011
    Posts
    58
    I'm inclined to do so, already

  4. #19
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Dasher View Post
    NO -- Use php to create the rss.xml file, by writing it to the server, as I mentioned earlier. It is a simple process.

    Just name your current file rss_generator.php and put all the xml statement into a the header or content. This is what I have done...
    This may work if you are only generating static RSS feeds. If you plan to have RSS feeds that might be specific to users or generally need to be generated on the fly then I would suggest an rss.php that can build the rss on the fly. Then in your RSS reader you just give the URL of yoursite.com/rss.php and the RSS reader will display appropriately (it doesn't care what the "extension" is in the URL in other words).

  5. #20
    Join Date
    May 2011
    Posts
    58
    i'm finally do it!!! I want to say all people who try to help me and answer my dumb questions thank you very much
    if someone need that is my work code: this file name rss.php
    PHP Code:
    <?php 
    echo '<?xml version="1.0" encoding="ISO-8859-1" ?>';

    @include_once(
    "./cfg.inc.php");

    $link mysql_connect($DBSERVER$DBUSER$DBPASS)
    or die(
    "Can't connect" );

    mysql_select_db($DB$link) or die ("Can't select DB");
    echo 
    '<rss version="2.0">
    <channel>
    <title>My title</title>
    <description>My description</description>
    <link>My site link</link>
           
    $query= mysql_query("SELECT `link`, `description`, `title`, UNIX_TIMESTAMP(`date`)  FROM `rss` ORDER BY `date` DESC LIMIT 0,25");


    while($result = mysql_fetch_array($query)){
    $title = htmlentities(strip_tags($result['
    title'],'ENT_QUOTES'));
    $link = htmlentities(strip_tags($result['
    link']);
    $date = strftime( "%a, %d %b %Y %T %Z" , $result['
    date']);
    echo '
    <item>
            <
    title>'.$title.'</title>
            <
    description>'.$description.'</description>
            <
    link>'.$link.'</link>
           
            <
    pubDate>'.$date.'</pubDate>
         </
    item>';
    }

    echo '
    </channel>
    </
    rss>';

    ?>
    i try this in Safari and Google Reader it works fine. One more time thank you all.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles