我试图将一些POST数据从Objective-c传递给PHP,但PHP文件似乎并没有获取数据。
以下是我发送的方式:
- (void) sendJSONData:(NSDictionary *)dictData {
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:kRootWebService];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSLog(@" URL: %@", request);
NSLog(@" Body: %@", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
NSLog(@"%@", httpResponse.allHeaderFields);
if (httpResponse.statusCode == 200) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"This is my print out of data to string\n%@",str);
//deseriealize the return
id JSONObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary* dictJSON;
NSArray* arrJSON;
NSLog(@"%@", dictJSON);
} else {
NSLog(@"There was an error connecting with the server, the status code returned was %i", (int)httpResponse.statusCode);
}
}];
[postDataTask resume];
}
这里是URL的日志(URL正确)和正文:
URL: http://xxxxxxxx.com/xxxxxx.php }
2017-07-30 15:20:10.602 MirrorBox[89680:5646406] Body: {"name":"TEST IOS","typemap":"IOS TYPE"}
这里是请求标题的日志
2017-07-30 15:20:10.751 MirrorBox[89680:5646478] {
Age = 0;
Connection = "keep-alive";
"Content-Length" = 34;
"Content-Type" = "text/html";
Date = "Sun, 30 Jul 2017 19:20:10 GMT";
Server = "Apache/2";
"X-Powered-By" = "PHP/5.5.22";
}
这是返回数据的日志,错误消息是我自己的:
This is my print out of data to string
{"key":"could not read POST data"}
这是PHP文件:
<?php
if (!$_POST['IOS_TYPE']) {
echo json_encode(array("key" => "could not read POST data"));
} else {
echo json_encode(array("key" => "returned string"));
}
?>
答案 0 :(得分:1)
如果您使用密钥传递POST,则应使用相同的密钥
进行访问 <?php
if (!$_POST['typemap']) {
echo json_encode(array("typemap" => "could not read POST data"));
} else {
echo json_encode(array("typemap" => "returned string"));
}
?>
或更好
if (isset($_POST['typemap'])){
if ($_POST['typemap'] == 'IOS TYPE' ) {
echo 'IOS TYPE type in typemap POST';
}
}
.....
答案 1 :(得分:1)
由于您在请求正文中传递JSON,请尝试以下方式:
$content = file_get_contents('php://input');
$obj = json_decode($content , false); // decode to stdObj
$typemap = $obj->typemap;
或者如果您更喜欢使用关联数组:
$ary = json_decode($content , true); // decode to array
$typemap = $ary['typemap'];