我正在爲一個站點創建一個RSS源。UTF8生成PHP SimpleXML的錯誤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();
在此先感謝...
倫納德
您是否已將[MySql連接編碼](http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html)設置爲UTF8爲好? – Jon 2012-01-09 23:47:20
@Jon是的。 mysql_client_encoding()返回'utf8' – 2012-01-09 23:52:43
您確定您正在使用UTF-8連接到數據庫嗎?在建立連接後第一次執行此查詢:mysql_query(「SET NAMES'utf8'」); – 2012-01-09 23:54:52