How do I respond to a POST from an iPhone app in PHP?
I have Objective C code creating an NSUrlConnection as follows:
//prepar request
NSString *urlString = [NSString stringWithFormat:@"http://ctruman.info/post.php"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//create the body
NSString *formData = [[NSString alloc] initWithFormat:@"%@ %@", username.text, password.text];
NSData *postData = [[NSString stringWithString:formData] dataUsingEncoding:NSUTF8St开发者_如何学CringEncoding];
//post
[request setHTTPBody:postData];
//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);
}
Then I have a php script that is supposed to read my POST variables that are being sent:
<?php
print('<pre>');
print_r($_POST);
print('</pre>');
?>
When I execute this, NSLog spits out the following:
Array ( )Why is it not printing out my post variables? Am I making the POST request incorrectly?
As it looks to me, PHP is trying to read your POST submission as if it were a properly formatted PHP POST payload. Instead, you're setting the content-type to XML content -- which probably confuses the heck out of PHP. It's looking for encoded variables, and finding XML.
You've got 2 options:
Read in the XML and parse it yourself using PHP: $xml = file_get_contents('php://input'); an example for reading input is here: http://www.codediesel.com/php/reading-raw-post-data-in-php/ then you can parse it with PHP's xml support: http://us.php.net/xml
Recode your objective-C to just send normal POST parameters up to the server. I use the ASIHTTPRequest libraries and it's easy. http://allseeing-i.com/ASIHTTPRequest/
The following code is working properly.
Objective-C
NSString *name = @"Anne"; // encode this
NSString *pass = @"p4ssw0rd"; // encode this
NSString *requestString = [NSString stringWithFormat:@"&name=%@&pass=%@",name,pass];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost/post.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *resultString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",resultString);
PHP
<?php
print_r($_POST);
?>
Result
Array
(
[name] => Anne
[pass] => p4ssw0rd
)
Alternative
Checkout ASIHTTPRequest:
http://allseeing-i.com/ASIHTTPRequest/
精彩评论