0

我在 Objective-C 应用程序中有一个 JSON 字符串,我想将它发送到服务器上的 PHP 脚本。

我应该使用什么 PHP 代码来接收和解析这些数据?

- (IBAction)send:(id)sender{

NSString *jsonString = [selectedPerson.jsonDictionary JSONRepresentation];


NSLog(@"ESE ES EL JSON %@", jsonString);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init    ];
NSString*post = [NSString stringWithFormat:@"&json=%@", jsonStri    ng];
NSData*postData = [post dataUsingEncoding:NSASCIIStringEncoding     allowLossyConversion:NO];

NSLog(@"ESTO ES DATA %@", postData);

[request setURL:[NSURL URLWithSt    ring:@"http://www.mydomine.com/recive.php"]];    
[request setHTTPMethod:@"POST"];    
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-    type"];
[request setHTTPBody:postData];
}
4

2 回答 2

2

如果你看:

NSString*post = [NSString stringWithFormat:@"&json=%@", jsonString];

您将看到 JSON 将包含在名为 json 的 POST 变量中。

接收.php

$json = $_POST['json'];
$decoded = json_decode($json);
于 2013-01-25T21:18:43.057 回答
1
If your are sending your JSON in POST method , It can be received in PHP with the below code

<?php $handle = fopen('php://input','r');
                $jsonInput = fgets($handle);
                // Decoding JSON into an Array
                $decoded = json_decode($jsonInput,true);
?>
于 2013-06-12T17:55:45.880 回答