UTF8 errors when generating PHP SimpleXML RSS feeds

I am creating an RSS feed for the site.

I am using SimpleXML to create an XML structure. When I call $ xml-> asXML () ;, it gives a lot of warnings:

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

I'm not sure what kind of mistake this is. The database table that she reads is utf8_general_ci. I tried to run utf_encode on a line that messed up the lines, rather than fixing them.

 //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 MYH: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 MYH:i:s T", $node->published_at)); } header('Content-Type: application/xhtml+xml'); echo $xml->asXML(); 

Thanks in advance...

Leonard

+4
source share
1 answer

I was able to reproduce your problem by replacing your $ nodes ... snippet with

 class myNode { public $title="(β•―Β°β–‘Β°οΌ‰β•―οΈ΅ ┻━┻"; public $description="dscr"; public $published_at=0; public function __construct(){ $this->published_at=time(); } } $nodes = array(new myNode()); 

Just deleting calls in htmlentities seems to work fine. (The output was correctly escaped as character objects)

+2
source

Source: https://habr.com/ru/post/1390139/


All Articles