I have looked at all the similar questions I could find but none seem to have all the right pieces together for my situation, which seems very simple.
- I'm creating a simple NSURLRequest using POST and setting its body to an NSDATA build using the NSJSONSerialization class.
I'm testing a very simple php script to capture that data.
The code works fine if I get the value in PHP using json_decode(file_get_contents('php://input'),1); but not at all using $_POST
For security reasons, I'm told the server cannot enable file_get_contents() ... so I'm looking for an alternative.
Here is my Obj-C code to create the request:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.techno-mac.org/dev101/jsonForm.php"]];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:self.userNameField.text forKey:@"userName"];
NSLog(@"Dict: %@",jsonDict);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString);
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSLog(@"jsonData: %@",jsonData);
self.postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
Here is the console log when I run it, typing "test" in a field that gets encoded in the json data:
2012-05-17 20:26:19.417 RBWebServices[95037:f803] Dict: {
userName = test;
}
2012-05-17 20:26:19.418 RBWebServices[95037:f803] JSON String: {"userName":"test"}
2012-05-17 20:26:19.418 RBWebServices[95037:f803] jsonData: <7b227573 65724e61 6d65223a 22746573 74227d>
2012-05-17 20:26:19.464 RBWebServices[95037:f803] Received response from server, resetting data length to zero
2012-05-17 20:26:19.465 RBWebServices[95037:f803] Succeeded! Received 12 bytes of data
And here is my very simple php script that works:
$json_post = json_decode(file_get_contents('php://input'),1);
$myArray = $json_post["userName"];
print $myArray;
and the code that doesn't (Returns "Array")
$json_post = json_decode($_POST);
print $json_post["userName"];
If I enter this in php:
var_dump($_POST)
I receive:
array(0){}
Because the first scenario works, I tend to think it is the $_POST that is empty which is the cause... or the way I'm trying to get something out of it, but it seems completely empty.
If this is impossible using $_POST, perhaps there is another way not to use file_get_contents ?
I'm far from being a php expert at all... sorry if this seems trivial.
Thanks for any suggestions you may have.