简体   繁体   中英

Using PHP to generate an XML file

I currently have a PHP script which when ran outputs an XML file (however as feed.php not feed.xml) I have been told to add this to my .htaccess and it should fix it:

AddType application/x-httpd-php .xml

However for some reason that isn't working.

What I'd ideally like the script to do - when ran is to generate a feed.xml file with the output of its contents, rather than just outputting the contents on the php page.

Here is my code:

<?PHP
 include("../config.php");
 #// Timetable Clearup Variabls
$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);
$order = "SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time";
$result = mysql_query($order);
$yesterdayd = date('F jS, Y', time()-86400);

    //SET XML HEADER
    header('Content-type: text/xml');

    //CONSTRUCT RSS FEED HEADERS
    $output = '<rss version="2.0">';
    $output .= '<channel>';
    $output .= "<title>Timetable - {$yesterdayd} </title>";
    $output .= '<description>Timetable.</description>';
    $output .= '<link>http://site.com/</link>';
 ###   $output .= '<copyright>Your copyright details</copyright>';
 while ($row = mysql_fetch_array($result)) {
    //BODY OF RSS FEED
   $output .= '<item>';
     $output .= "<description><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></description>";
   $output .= '</item> ';
 }
    //CLOSE RSS FEED
   $output .= '</channel>';
   $output .= '</rss>';

    //SEND COMPLETE RSS FEED TO BROWSER
    echo($output);

?>

I would suggest not adding .xml in .htaccess to be processed using PHP compiler. Rather use mod_rewrite to redirect requests to .xml files to php script.

For example:

RewriteEngine On
RewriteRule ^(.*)\.xml$ /rss_feed.php [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM