我正在为站点创建 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();
提前致谢...
伦纳德
最佳答案
我能够重现您的问题,将您的 $nodes ... 片段替换为
class myNode {
public $title="(╯°□°)╯︵ ┻━┻";
public $description="dscr";
public $published_at=0;
public function __construct(){
$this->published_at=time();
}
}
$nodes = array(new myNode());
简单地删除对 htmlentities 的调用似乎工作正常。 (输出被正确转义为字符实体)
关于php - 生成 PHP SimpleXML RSS 提要时出现 UTF8 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796779/