0
使用php生成一個xml rss提要。rss feed xml ISO-8859-7編碼將希臘字符顯示爲問號
PHP代碼:
<?php
header("Content-Type: application/rss+xml; charset=utf-8");
$rssfeed = '<?xml version="1.0" encoding="utf-8"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>Events on Spotlight | StudentLife.com.cy</title>';
$rssfeed .= '<link>http://spotlight.studentlife.com.cy</link>';
$rssfeed .= '<description>All the events that student may be interested in.</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) 2014 studentlife.com.cy</copyright>';
$today= strtotime("now");
$today=date('Y-m-d',$today);
$later= strtotime("+1 week");
$later=date('Y-m-d',$later);
$db = mysql_connect($db_host, $db_user, $db_pass);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name,$db);
$result = mysql_query("SELECT p.ID, m1.meta_value as event_address, m2.meta_value as event_date, p.post_title,p.guid as link
from events_posts p
inner join events_postmeta m1
on p.ID=m1.post_id and p.post_type='event' and (p.post_status='publish' or p.post_status='recurring')
and m1.meta_key='address'
inner join events_postmeta m2
on p.ID=m2.post_id and p.post_type='event' and (p.post_status='publish' or p.post_status='recurring')
and m2.meta_key='st_date'
and m2.meta_value between '".$today."' and '".$later."'
order by p.ID", $db);
while($row = mysql_fetch_array($result)) {
extract($row);
$rssfeed .= '<item>';
$rssfeed .= '<title>' . $post_title . '</title>';
$rssfeed .= '<description>' . $event_address . '</description>';
$rssfeed .= '<link>' . $link . '</link>';
$rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($event_date)) . '</pubDate>';
$rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
mysql_close($db);
$rssfeed=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $rssfeed);
$fp = fopen("rss.xml","wb");
fwrite($fp,$rssfeed);
fclose($fp);
?>
編碼在我的數據庫是utf8_bin。您可以在http://studentlife.com.cy/cron.php執行該文件並在http://studentlife.com.cy/rss.xml處查看結果。
的XML給我的編碼錯誤
你說瀏覽器,你迴應/ XML是在這個字符集。這並不意味着你輸出的字符串真的是這樣編碼的。 – ThW
那麼我應該挖掘我的數據庫來查找字符串的編碼嗎? – Onisiforos
數據庫連接的編碼將是首先看的地方。但是,如果有可能在腳本中使用UTF-8,並且只有在需要輸出不同的字符集時,纔可以在最後時刻轉換數據。 Unicode可以包含更多的字符,然後是ANSI字符集。 – ThW