当我需要UTF-8时,PHP以ASCII格式输出XML [英] PHP Outputs XML in ASCII when I need UTF-8
问题描述
好吧,我还有一个问题,那么我认为程序的PHP部分将准备就绪.谢谢大家到目前为止给我的帮助!
Ok, I have one more question then I think the PHP part of my program will be ready to rock and roll. Thank you everyone for the help you've given me so far!
我将以下PHP文件上传到网站上,以充当我的应用程序的网络服务,以从MySQL数据库检索数据并以XML形式返回我的应用程序中所需的内容.我认为它的格式正确,并且是我想要的格式(尽管当然欢迎任何输入).片段后,我将要解决的问题.
I have the following PHP file uploaded to a website to act as a web service for my app to retrieve data from a MySQL Database and return the things that I need in my app in the form of XML. I think it is formatted correctly and is how I would like it (though any and all input is of course welcome). I'll get to the problem I'm having after the snippet.
要查看输出,只需对该URL进行cURL(与我在NSURL中调用的URL相同): http://jeffstockdale.com/API/get_blog_posts.php
To see the output, simply cURL this URL (it's the same URL I'm calling in NSURL): http://jeffstockdale.com/API/get_blog_posts.php
PHP文件如下
<?php
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
$sql = "SELECT post_date_gmt, post_content, post_title FROM schema.wp_posts WHERE post_status = \"publish\" && post_type = \"post\" ORDER BY post_date_gmt DESC;";
$res = mysql_query($sql);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('BlogPosts');
while ($row = mysql_fetch_assoc($res)) {
$xml->startElement("Post");
$xml->startElement("PostTitle");
$xml->text($row['post_title']);
$xml->endElement();
$xml->startElement("PostDate");
$xml->text($row['post_date_gmt']);
$xml->endElement();
$xml->startElement("PostContent");
$xml->writeCData($row['post_content']);
$xml->endElement();
$xml->endElement();
}
$xml->endElement();
header('Content-type: text/html; charset=utf-8');
$xml->flush();
?>
现在是问题所在.我进入我的Xcode项目,创建一个NSURL(文件下载正确)并将其移交给我的NSXMLParser,然后从parseErrorOccurred中得到以下错误:
And now to the problem. I go into my Xcode Project, create a NSURL (the file downloads correctly) and hand it over to my NSXMLParser, which then gives me the following error from parseErrorOccurred:
2014-10-09 15:03:41.114 AppName [8667:521529] NSXMLParser遇到错误:Error Domain = NSXMLParserErrorDomain Code = 9操作无法完成.(NSXMLParserErrorDomain错误9.)" UserInfo = 0x7fbddd0b1a10 {NSXMLParserErrorColumn = 44,NSXMLParserErrorLineNumber = 1352,NSXMLParserErrorMessage =输入不正确的UTF-8,请指示编码! 字节:0x85 0x3C 0x2F 0x50 }
2014-10-09 15:03:41.114 AppName[8667:521529] NSXMLParser encountered an error: Error Domain=NSXMLParserErrorDomain Code=9 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 9.)" UserInfo=0x7fbddd0b1a10 {NSXMLParserErrorColumn=44, NSXMLParserErrorLineNumber=1352, NSXMLParserErrorMessage=Input is not proper UTF-8, indicate encoding ! Bytes: 0x85 0x3C 0x2F 0x50 }
我认为这是因为我的PHP文件需要以UTF-8格式返回其ASCII数据.所以我的问题是,如何将其输出到UTF-8,以便NSXMLParser可以解析它?
I'm taking that as my PHP file is returning it's data in ASCII when it needs to be in UTF-8. So my question is, how do I either get it to output to UTF-8 so NSXMLParser can parse it?
推荐答案
您的MySQL数据库似乎不包含UTF-8编码的数据.结果,它的输出不是UTF-8,而是其他一些编码(也许是iso-latin1?但绝对不是ASCII).您可以将编码从任何编码重新编码为UTF-8以进行显示(请参见PHP文档,例如
It seems that your MySQL database does not contain UTF-8 encoded data. As a result it's output is not in UTF-8 but rather some other encoding (maybe iso-latin1? but definitely not ASCII). You may either re-encode from whatever encoding it is to UTF-8 for display (see the PHP doc, e.g. mb_convert_encoding), or re-encode your database.
对于第一个解决方案,只需包装从SQL查询获得的数据,如下所示:
For the first solution, simply wrap the data obtained from the SQL query like so:
...
$xml->text(mb_convert_encoding($row['post_title'], "UTF-8", "ISO-8859-1"));
// replace ISO-8859-1 (aka latin1) with the encoding actually used
...
更新:XMLWriter需要使用UTF-8字符串作为输入.因此,即使使用startDocument
声明编码,它也不能接受其他编码.有关更多信息,请参见其他问题.因此,上面总是有两个解决方案.
Update: XMLWriter requires UTF-8 strings as input. So it cannot accept other encoding even if declaring the encoding with startDocument
. See this other question for more information. So there's always the two solutions above.
这篇关于当我需要UTF-8时,PHP以ASCII格式输出XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!