exhaler Posted September 20, 2009 Share Posted September 20, 2009 hi, i recently added a RSS section for a website. i did this using PHP: <?php header("Content-Type: application/rss+xml; charset=windows-1252"); // title of the rss feed $rssfeed = "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n"; $rssfeed .= "<rss version=\"2.0\">\n"; $rssfeed .= "<channel>\n"; $rssfeed .= "<title>domain.com Featured </title>\n"; $rssfeed .= "<link>http://www.domain.com</link>\n"; $rssfeed .= "<description>A professional directory. Looking for a restaurant? Find it easily, search by name & even street location.</description>\n"; $rssfeed .= "<language>en-us</language>\n"; $rssfeed .= "<copyright>Copyright (C) 2009 domain.com</copyright>\n"; // image for the rss feed $rssfeed .= "<image>\n"; $rssfeed .= "<title>domain.com</title>\n"; $rssfeed .= "<link>http://www.domain.com</link>\n"; $rssfeed .= "<url>http://www.domain.com/images/rssicon.jpg</url>\n"; $rssfeed .= "<width>144</width>\n"; $rssfeed .= "<height>45</height>\n"; $rssfeed .= "<description>A professional directory. Looking for a restaurant? Find it easily, search by name & even street location.</description>\n"; $rssfeed .= "</image>\n"; $query_critics = "SELECT * FROM featured ORDER BY id DESC"; $result_critics = mysql_query($query_critics); while($row_critics = mysql_fetch_array($result_critics)) { $rest_name = stripslashes($row_critics['name']); $description = stripslashes($row_critics['part_desc']); $id = $row_critics['id']; $link = "http://www.domain.com/readfeatured.php?id=$id"; $date = stripslashes($row_critics['date']); $pic = stripslashes($row_critics['pic']); $rssfeed .= "<item>\n"; $rssfeed .= "<title>" . "New Featured Restaurant: " . $rest_name . "</title>\n"; $rssfeed .= "<guid>" . $link . "</guid>\n"; if (!empty($pic)) { $image = "<p><a href=\"$link\"><img src=\"http://domain.com/$pic\" align=\"left\" height=\"80\" width=\"80\" alt=\"\" border=\"0\" /></a>"; $rssfeed .= "<description>" . $image . $description . "</p><br clear=\"all\"/></description>\n"; } else { $rssfeed .= "<description>" . $description . "</description>\n"; } $rssfeed .= "<link>" . $link . "</link>\n"; $rssfeed .= "<pubDate>" . date("D, d M Y H:i:s O", strtotime($date)) . "</pubDate>\n"; $rssfeed .= "</item>\n"; } $rssfeed .= "</channel>\n"; $rssfeed .= "</rss>"; echo $rssfeed; // Close connection mysql_close($connection); ?> the above code works perfectly, but i was wondering if i can make PHP write to rss file. Most of the websites don't have a RSS that ends in .php but ends in .rss. having it in .rss reduces the load on the server to compile the php file agian. any ideas how to make the code above write to an rss file? thanks Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 20, 2009 Share Posted September 20, 2009 I do know a lot of sites allow .rss to read .php, so simply naming your file would make it appear as .rss only That said, you can always look at using fopen() , fwrite() etc.. Quote Link to comment Share on other sites More sharing options...
exhaler Posted September 20, 2009 Author Share Posted September 20, 2009 thanks ozestretch, i'll try renaming the file from .php to .rss. but is there a better way to do this in PHP, like using a RSS parser? Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 20, 2009 Share Posted September 20, 2009 thanks ozestretch, i'll try renaming the file from .php to .rss. but is there a better way to do this in PHP, like using a RSS parser? you will need to make an exception so your server reads the php in the .rss. Usually done with .htaccess in your .htaccess AddType application/x-httpd-php .php .rss but it will still be dynamic (which is good if it is being updated regularly) Quote Link to comment Share on other sites More sharing options...
raccoon Posted September 20, 2009 Share Posted September 20, 2009 I heard PHP MagpieRSS is one of the most simple, easy, robust php classes for RSS. http://magpierss.sourceforge.net/ I haven't used it myself yet, but apparently it's great. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.