Jump to content

PHP to RSS


exhaler

Recommended Posts

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 = "&#60;p>&#60;a href=\"$link\">&#60;img src=\"http://domain.com/$pic\" align=\"left\" height=\"80\" width=\"80\" alt=\"\" border=\"0\" />&#60;/a>";	
    		$rssfeed .= "<description>" . $image . $description . "&#60;/p>&#60;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

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.