1

I'm fairly new to iOS development and want to send a request message to a web service that I created in PHP. It will take the XML request, process and then provide a response XML message.

However, the issue I'm having is that when sending data to the Webservice it is in NSData form.

NSLog of the data being sent is:

<3c3f786d 6c207665 7273696f etc etc ... 743e>

However the PHP script is expecting an XML message like this:

<?xml version="1.0" ?><request-message><tag-1></tag-1><tag-2></tag-2></request-message>

So my question is, is there a way of sending the XML without converting to data, or is there a way to convert the NSData string to readable XML on the PHP Server Side?

Thanks in advance.

Pazzy

EDIT: To include request code:

// Construct the webservice URL
NSURL *url = [NSURL URLWithString:@"http://localhost/web/check_data.php"];

NSString *requestXML = @"<?xml version='1.0'?><request-message><tag-1>VALUE1</tag-1><tag-2>VALUE2</tag-2></request-message>";

NSData *data = [requestXML dataUsingEncoding:NSUTF8StringEncoding];

// Create a request object with that URL
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
Pazzy
  • 11
  • 3

1 Answers1

4

Send the XML on the HTTP Body and parse it in the PHP side and you need to set the Content-Type To application/xml; charset=utf-8:

NSString* sXMLToPost = @"<?xml version=\"1.0\" ?><request-message><tag-1></tag-1><tag-2></tag-2></request-message>";

NSData* data = [sXMLToPost dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:@"http://myurl.com/RequestHandler.ashx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[sXMLToPost dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

if (error) {..handle the error}

and on the server try the following PHP code:

$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
    $http_raw_post_data .= fread($handle, 8192);
}
fclose($handle);

Have a look at this iPhone sending POST with NSURLConnection

Community
  • 1
  • 1
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • Thanks ... your code doesn't look much different to mine. I have added the [request setValue:@"application/xml; charset=..."]; Not sure if its the Obj-C code ... – Pazzy May 15 '11 at 21:10
  • Are getting your request on the server sucessfully ? – Ahmad Kayyali May 15 '11 at 21:13
  • Not entirly sure ... I've set the script to now output what is sent to it and allow the connectDidFinish to print to the console what is returned. Nothing is being returned. From the PHP side, i have two lines to output this: $rawPostXML = file_get_contents("php://input"); echo $rawPostXML; – Pazzy May 15 '11 at 21:23
  • `connectDidFinish`? why are you using that? are you sure you are using `NSURLConnection sendSynchronousRequest:`? – Ahmad Kayyali May 16 '11 at 06:10