简体   繁体   English

使用PHP生成XML文件

[英]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: 我目前有一个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);

?>

I would suggest not adding .xml in .htaccess to be processed using PHP compiler. 我建议不要在.htaccess中添加.xml来使用PHP编译器进行处理。 Rather use mod_rewrite to redirect requests to .xml files to php script. 而是使用mod_rewrite将.xml文件的请求重定向到php脚本。

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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