0

I have done a basic http post to a server and received XML data back, so that is good.

So then, I decided to install MAMP and create my first php page ;-)

From all of the googling I have done, I suspect that it is a utf-8/encoding/newline issue, but it is driving my bonkers ;-)

The php server gets the http post data, but can't convert into XML so it is failing at that point (see debug at end and variable is empty).

thanks for any suggestions!

Here is the objective-c:

NSString *postString = [NSString stringWithString:@"xml=<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"];
postString = [postString stringByAppendingString:@"<note>\r\n"];
postString = [postString stringByAppendingFormat:@"<tasks>%@</tasks>\r\n", @"task1234"];
postString = [postString stringByAppendingFormat:@"<details>%@</details>\r\n", @"det1"];
postString = [postString stringByAppendingString:@"</note>\r\n"];

NSURL *url = [NSURL URLWithString:@"http://localhost:8888/xmltest.php"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];

[req addValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req setHTTPMethod:@"POST"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

And here is the PHP:

<?php
$form_input = $_POST['xml'];  
$objDOM = new DOMDocument();
$objDOM->loadXML($form_input);
echo "here1: $form_input \n";

$note = $objDOM->getElementsByTagName("note");
echo "here2\n";
print_r ($note);

foreach( $note as $value )
{
  echo "here3\n";
  $tasks = $value->getElementsByTagName("tasks");
  $task  = $tasks->item(0)->nodeValue;
  echo "$task :: $detail <br>";
}

And the debug output:

here1: <?xml version=\"1.0\" encoding=\"utf-8\"?>
<mynotes>
<note>
<tasks>task1234</tasks>
<details>details1234</details>
</note>
</mynotes>

here2
DOMNodeList Object
(
)
user589310
  • 51
  • 7

1 Answers1

0

Try ASIHTTPRequest

jiangyan
  • 28
  • 1