PHP生成RSS XML - 编码错误

时间:2022-04-08 12:00:38

So I have a small PHP script that generates an xml feel for the rss feed of my blog. However, it throws up this error:

所以我有一个小的PHP脚本,可以为我博客的RSS源生成xml感觉。但是,它会引发此错误:

This page contains the following errors:

此页面包含以下错误:

error on line 23 at column 14: Encoding error
Below is a rendering of the page up to the first error.

第14行第23行的错误:编码错误下面是第一个错误之前的页面呈现。

(seen here: http://aviatex14.co.uk/rss.xml)

(见此处:http://aviatex14.co.uk/rss.xml)

Here's the code that generates it:

这是生成它的代码:

while ($line = mysql_fetch_array($result)) {
  $return[] = $line;
  var_dump(date("Y-m-d H:i:s", $line['timestamp']));
}

$now = date("D, d M Y H:i:s T");

$output = "<?xml version=\"1.0\" encoding=\"UTF-16\" ?>
             <rss version=\"2.0\">
                <channel>
                  <title></title>
                  <link></link>
                  <description></description>
                  <pubDate></pubDate>
                  <language>en-us</language>

                  <lastBuildDate>$now</lastBuildDate>
          ";

foreach ($return as $line) {
  $output .= "<item><title>".htmlentities($line['title'])."</title>
                <link>http://blog.aviatex14.co.uk/permalink.php?uid=".htmlentities($line['uid'])."</link>     
                <description>".htmlentities(strip_tags($line['entry']))."</description>
                <pubDate>".$date = date("Y-m-d H:i:s T", $line['timestamp'])."</pubDate> 
              </item>";
}
$output .= "</channel></rss>";
print "Content-Type: application/rss+xml";
echo $output;

$f = fopen("rss.xml", "w"); 
fwrite($f, $output); 
fclose($f); 

Any help would be appreciated! :D

任何帮助,将不胜感激! :d

2 个解决方案

#1


6  

It says "TOKYO � A Japanese" at that line (and further down in the feed as well). The � is not utf-8. Try to utf8_encode (or iconv if you want a different encoding) the content or even better: use an XML processor to create the feed.

它在那条线上说“TOKYO A Japanese”(并且在饲料中也是如此)。 不是utf-8。尝试使用utf8_encode(或iconv,如果你想要一个不同的编码)内容甚至更好:使用XML处理器来创建feed。

#2


1  

You have to configure Your database or connection. Try to execute mysql_set_charset('utf8'); after connection to database.

您必须配置您的数据库或连接。尝试执行mysql_set_charset('utf8');连接到数据库后。

http://php.net/manual/en/function.mysql-set-charset.php

http://php.net/manual/en/function.mysql-set-charset.php

P.S. You should use <?xml version="1.0" encoding="UTF-8" ?> or drop that line. Your output looks like 8 bits based.

附:你应该使用<?xml version =“1.0”encoding =“UTF-8”?>或删除该行。您的输出看起来像8位。

#1


6  

It says "TOKYO � A Japanese" at that line (and further down in the feed as well). The � is not utf-8. Try to utf8_encode (or iconv if you want a different encoding) the content or even better: use an XML processor to create the feed.

它在那条线上说“TOKYO A Japanese”(并且在饲料中也是如此)。 不是utf-8。尝试使用utf8_encode(或iconv,如果你想要一个不同的编码)内容甚至更好:使用XML处理器来创建feed。

#2


1  

You have to configure Your database or connection. Try to execute mysql_set_charset('utf8'); after connection to database.

您必须配置您的数据库或连接。尝试执行mysql_set_charset('utf8');连接到数据库后。

http://php.net/manual/en/function.mysql-set-charset.php

http://php.net/manual/en/function.mysql-set-charset.php

P.S. You should use <?xml version="1.0" encoding="UTF-8" ?> or drop that line. Your output looks like 8 bits based.

附:你应该使用<?xml version =“1.0”encoding =“UTF-8”?>或删除该行。您的输出看起来像8位。