0

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?

Kirkland
  • 798
  • 1
  • 8
  • 20
  • That's definitely not ASCII. For what it's worth ASCII would have been accepted as UTF-8. – Po' Lazarus Oct 09 '14 at 20:46
  • Do you have any idea why it might be giving me this error then? – Kirkland Oct 09 '14 at 20:48
  • Have you checked that the database connection encoding is utf-8? – ThW Oct 09 '14 at 21:06
  • ThW Per your question, I looked via MySQL Workbench and confirmed that the db is encoding UTF-8. – Kirkland Oct 09 '14 at 21:39
  • @Kirkland: That's only the meta-data your Database, Table and Columns might have set. It must not mean that all data is UTF-8 encoded. NSXML tells you it is not. Better check it before you pass the XML out. – hakre Oct 10 '14 at 13:51
  • See as well: [How to detect malformed utf-8 string in PHP?](http://stackoverflow.com/q/6723562/367456) – hakre Oct 10 '14 at 13:57

3 Answers3

1

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.

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
...

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.

Community
  • 1
  • 1
Po' Lazarus
  • 477
  • 5
  • 13
0

What it looks like is you need to declare your encoding type in the XMLwriter.

Like so $xml->startDocument('1.0','utf-8'); That way your XML encoding is declared when NSXML parses it.

If it was your data input then XMLwriter would throw an error cause it only accepts UTF-8.

wintheday
  • 139
  • 9
  • 1
    In the absence of declared encoding, NSXMLParser already expects UTF-8, as the error message clearly says the xml is not encoding. – Po' Lazarus Oct 09 '14 at 20:57
  • 2
    well it can't be his input because XMLWriter only accepts UTF-8 as input without throwing errors. – wintheday Oct 09 '14 at 21:02
  • I don't think XMLWriter validate anything. If you don't declare an encoding or if you declare 'utf-8', your input are passed through it untouched (that explains the problem above). Otherwise, they are reencoded from utf-8 to whatever you choose, which failed miserably if your input is not utf-8. – Po' Lazarus Oct 09 '14 at 21:14
  • I've made the adjustments to the PHP file and I'm still getting the same error. Do we perhaps think that the error might be in my parsing code and not in the the XML that this PHP document returns? – Kirkland Oct 09 '14 at 21:42
  • Have you tried just writing the XML to a file to make sure that XMLWriter is getting good data and not throwing an error? – wintheday Oct 09 '14 at 21:48
  • Sorry, I lost the functioning PHP and had to write it again. I just finished and I'm going to append a cURL to the address to an output.xml file and try parsing that and see if I get issues. Be right back. – Kirkland Oct 09 '14 at 22:23
  • It's still having trouble with parsing the XML file (when local). Assuming there is nothing wrong with the XML file (which everyone seems to be mostly in agreement that it has to be outputting valid XML that is in UTF-8), that means the problem is in my parser, not the XML itself. I'm going to post a new question specific to my Objective-C Code, as the problem seems to have changed. I'm no longer getting an error, however NSXMLParser doesn't seem to run (and doesn't return an error). Thank you everyone for your assistance! – Kirkland Oct 09 '14 at 22:39
0

The XML that is generated by this PHP script is valid. The problem does not seem to reside in this part of my program. Thank you to everyone for the very useful information.

enter image description here

Kirkland
  • 798
  • 1
  • 8
  • 20