UTF8生成PHP SimpleXML RSS提要时出错

时间:2012-01-09 23:44:28

标签: php rss simplexml

我正在为网站创建RSS Feed。

我使用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();

提前致谢...

伦纳德

1 个答案:

答案 0 :(得分:2)

我能够重现您的问题,用

替换您的$ nodes ...代码段
class myNode {

    public $title="(╯°□°)╯︵ ┻━┻";
    public $description="dscr";
    public $published_at=0;

    public function __construct(){
        $this->published_at=time();
    }

}

$nodes = array(new myNode());

简单地删除对htmlentities的调用似乎工作正常。 (输出已正确转义为字符实体)