This question already has an answer here:
- Encoding issues with XMLWriter (PHP) 1 answer
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!
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.
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
The PHP file looks like this
<?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();
?>
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 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 }
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?
</div>