Reputation: 11
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"];
Upvotes: 1
Views: 2181
Reputation: 8243
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
Upvotes: 4