使用PHP生成XML文件

时间:2022-11-23 08:12:55

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:

我目前有一个PHP脚本,在运行时输出一个XML文件(但是作为feed.php而不是feed.xml)我被告知要将它添加到我的.htaccess中它应该修复它:

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.

理想情况下,我喜欢脚本 - 当运行时,生成带有其内容输出的feed.xml文件,而不是仅仅输出php页面上的内容。

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);

?>

1 个解决方案

#1


3  

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.

我建议不要在.htaccess中添加.xml来使用PHP编译器进行处理。而是使用mod_rewrite将.xml文件的请求重定向到php脚本。

For example:

例如:

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

#1


3  

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.

我建议不要在.htaccess中添加.xml来使用PHP编译器进行处理。而是使用mod_rewrite将.xml文件的请求重定向到php脚本。

For example:

例如:

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