簡體   English   中英

使用PHP生成XML文件

[英]Using PHP to generate an XML file

我目前有一個PHP腳本,當運行時輸出一個XML文件(但是作為feed.php而不是feed.xml)我被告知要將它添加到我的.htaccess中它應該修復它:

AddType application/x-httpd-php .xml

但由於某種原因,這是行不通的。

理想情況下,我喜歡腳本 - 當運行時生成帶有其內容輸出的feed.xml文件,而不是僅僅輸出php頁面上的內容。

這是我的代碼:

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

?>

我建議不要在.htaccess中添加.xml來使用PHP編譯器進行處理。 而是使用mod_rewrite將.xml文件的請求重定向到php腳本。

例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM