如何从iOS发送的PHP中获取数组

以下是我的iOS代码发送NSmutablearrays到PHP的Web服务:

// Getting Server Address AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSString *serverAddress = [appDelegate getServerAddress]; serverAddress = [serverAddress stringByAppendingString:@"ABC.php"]; NSLog(@"Server Address: %@",serverAddress); NSData *post = [NSJSONSerialization dataWithJSONObject:UsersArray options:NSJSONWritingPrettyPrinted error:nil]; NSString *postLength = [NSString stringWithFormat:@"%d", [post length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:serverAddress]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:post]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setTimeoutInterval:30]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error){ //Do whatever with return data NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; NSLog(@"Result : %@",result); }]; 

我想在PHP中检索这个数组。 我怎样才能做到这一点?

这是我尝试的PHP,但返回null:

 $handle = fopen('php://input','r'); $jsonInput = fgets($handle); // Decoding JSON into an Array $decoded = json_decode($jsonInput,true); echo json_encode($decoded); 

我使用一个稍微不同的Content-Type (这应该不重要):

 [request addValue:@"text/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

我也使用一个稍微不同的PHP:

 <?php $handle = fopen("php://input", "rb"); $http_raw_post_data = ''; while (!feof($handle)) { $http_raw_post_data .= fread($handle, 8192); } fclose($handle); $json_data = json_decode($http_raw_post_data, true); echo json_encode($json_data); ?> 

如果你得到一个空白的答复,我敢打赌你有一些PHP错误。 我会检查你的服务器的错误日志,或者暂时改变你的php.ini中的display_errors设置,如下所示:

 display_errors = On