サイトのRSSフィードを作成しています。PHP SimpleXMLを生成する際のUTF8エラー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();
おかげで...
レナード私は
class myNode {
public $title="(╯°□°)╯︵ ┻━┻";
public $description="dscr";
public $published_at=0;
public function __construct(){
$this->published_at=time();
}
}
$nodes = array(new myNode());
だけへの呼び出しを削除するとスニペット...あなたの$ノードを交換し、あなたの問題を再現することができました
[MySql接続エンコーディング](http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html)をUTF8に設定しましたか?まあ? – Jon
@Jonはい。 mysql_client_encoding()は 'utf8'を返します –
あなたはデータベースへのUTF-8接続を使用していますか?接続を確立した後、初めてこのクエリを実行します:mysql_query( "SET NAMES 'utf8'"); –