rss feed xml ISO-8859-7编码将希腊字符显示为问号

时间:2014-02-25 16:25:44

标签: php xml encoding utf-8 rss

使用php生成xml rss feed。

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]+;)/', '&amp;', $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给了我一个编码错误

1 个答案:

答案 0 :(得分:0)

我要感谢ThW的评论。解决方案是使用mysqli连接到您的数据库,然后使用

将字符集连接到UTF-8
mysqli_set_charset($link, "utf8");

mysqli_set_charset()设置从数据库服务器发送数据时使用的默认字符集。

请参阅documentation