Problem: RSS file saved as .php not .XML

Discussion in 'PHP' started by junandya, Dec 29, 2007.

  1. #1
    Hi, i want to ask about my RSS script that generated by my php code. the problem is when user klick the link, it generates the file as .php not .XML. But the content is ok.

    here is the script:

    <?php
    include "conConfig/config.php";
    $connection=mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db,$connection);
    
    $query = "select title, author, text from `news` limit 15";
    $result = mysql_query($query, $connection);
    
    while ($line = mysql_fetch_assoc($result))
            {
                $return[] = $line;
            }
    
    $now = date("D, d M Y H:i:s T");
    
    $output = "<?xml version=\"1.0\"?>
                <rss version=\"2.0\">
                    <channel>
                        <title>News Title</title>
                        <author>Author</author>
                        <description>Description</description>
                        <language>en-us</language>
                        <pubDate>$now</pubDate>
                        <lastBuildDate>$now</lastBuildDate>
                        <docs>http://www.testBaelah.com</docs>
                        <managingEditor>sigokil@testBaelah.com</managingEditor>
                        <webMaster>web@testBaelah.com</webMaster>
                ";
    
    foreach ($return as $line)
    {
        $output .= "<item>
    					<title>".htmlentities($line['title'])."</title>
                        <author>".htmlentities($line['author'])."</author>
    					<description>".htmlentities(strip_tags($line['text']))."</description>
                    </item>";
    }
    $output .= "</channel></rss>";
    header("Content-Type: application/rss+xml");
    echo $output;
    ?>
    PHP:
    And this is the content of the result file if the link is clicked by visitor.

    RSS.php
    <?xml version="1.0"?>
    <rss version="2.0">
    	<channel>
    		<title>News Title</title>
    		<author>Author</author>
    		<description>Description</description>
    		<language>en-us</language>
    		<pubDate>Sun, 30 Dec 2007 10:05:32 SE Asia Standard Time</pubDate>
    		<lastBuildDate>Sun, 30 Dec 2007 10:05:32 SE Asia Standard Time</lastBuildDate>
    		<docs>http://www.testBaelah.com</docs>
    		<managingEditor>sigokil@testBaelah.com</managingEditor>
    		<webMaster>web@testBaelah.com</webMaster>
    		
    		<item>
    			<title>News this week</title>
    			<author>Jack</author>
    			<description>This the content</description>
    		</item>
    			<item>
    			<title>News this week 2</title>
    			<author>Darsimin</author>
    			<description>This the content 2</description>
    		</item>
    	</channel>
    </rss>
    PHP:
    My problem is, i want to make it generated as rss.xml not as rss.php, so it can be opened directly by web browser. I hope some one can help & solve my problem.

    Best Regards

    Sandy Junandya
     
    junandya, Dec 29, 2007 IP
  2. pratham

    pratham Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The browser looks for the content-type and renders accordingly, so you script can be anything you call it.
    You should perhaps change the MIME-type to text/xml or application/xml for all browsers to recognize it as a feed.
     
    pratham, Dec 29, 2007 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    .htaccess

    RewriteEngine On
    RewriteRule ^rss\.xml$ /rss\.php [L]
     
    Kaizoku, Dec 30, 2007 IP
  4. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Ow thanks for the answer, so it is nothing problem with my script...?
     
    junandya, Dec 30, 2007 IP
  5. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Put this after the include line:
    
    header('Content-Type: text/xml');
    
    PHP:
    and use the Kaizoku's .htaccess suggestion too, to be able to call it as rss.xml
     
    hogan_h, Dec 30, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    There is no need to change the extension.

    If the headers that are sent are XML, it will be viewed as XML by all browsers/feeders.

    Peace,
     
    Barti1987, Dec 30, 2007 IP