UTF8生成PHP SimpleXML RSS提要时出错


UTF8 Errors on generating PHP SimpleXML RSS feed

我正在为一个网站创建RSS提要。

我正在使用SimpleXML来创建XML结构。当我调用$xml->asXML();时;,它抛出了许多警告:

ErrorException [ Warning ]: SimpleXMLElement::asXML() [simplexmlelement.asxml]: string is not in UTF-8

我不确定这个错误是什么。它正在读取的数据库表是utf8_general_ci。我试着在字符串上运行utf_encode,它把字符串搞砸了,而不是修复它

//First create the XML root
$xml = new SimpleXMLElement('<rss version="2.0"></rss>');
//Create the Channel
$channel = $xml->addChild('channel');
//Construct the feed parameters
$channel->addChild('title', 'CGarchitect Feed');
$channel->addChild('link', Config::get('base_url'));
$channel->addChild('description', 'CGarchitect is the leading online community for architectural visualization professionals.');
$channel->addChild('pubDate', date("D, d M Y H:i:s T"));
//Get the feed items
$nodes = <....snip... >
foreach ($nodes as $node)
{
    //Parse the title and description
    $title = htmlentities(strip_tags($node->title));
    $description = htmlentities(strip_tags($node->description));
    $newItem = $channel->addChild('item');
    $newItem->addChild('title', $title);
    $newItem->addChild('description', $description);
    $newItem->addChild('pubDate', date("D, d M Y H:i:s T", $node->published_at));
}
header('Content-Type: application/xhtml+xml');
echo $xml->asXML();

提前感谢。。。

Leonard

我能够重现您更换$nodes的问题。。。带有的代码段

class myNode {
    public $title="(╯°□°)╯︵ ┻━┻";
    public $description="dscr";
    public $published_at=0;
    public function __construct(){
        $this->published_at=time();
    }
}
$nodes = array(new myNode());

简单地删除对htmlentities的调用似乎效果不错。(输出被正确地转义为字符实体)