php - 生成 PHP SimpleXML RSS 提要时出现 UTF8 错误

标签 php rss 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();

提前致谢...

伦纳德

最佳答案

我能够重现您的问题,将您的 $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/

相关文章:

php - 如何以编程方式在 magento 中创建贷项通知单

php - 在 PHP 代码中,如何删除换行符

ios - 使用 AFNetworking 2.0 时为 "unacceptable content-type: application/rss+xml"

ios - 阅读 RSS 提要的安全问题和想法

php - SimpleXML 和 foreach 循环中设置值的差异

PHP - 如何读取私有(private) XML 文件?

php - 使用 SimpleXML 加载 XML 不返回某些元素的属性

php - MYSQL + PHP 可变选择值

php - 在 WordPress 中创建自定义小部件

javascript - Node.js 异步模块在迭代完成之前完成