从iOS到PHP脚本的JSON数据

Dom*_*old 5 php iphone json ios

如何在php脚本中访问json数据,它是通过http-post收到的?我正在iOS上做以下事情:

NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/script.php"]];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:data];

[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
Run Code Online (Sandbox Code Playgroud)

如何在php脚本中访问此数据?换句话说,当我json_decode(...)在php脚本中调用时,是什么...

Nak*_*ran 14

如果您使用POST方法发送JSON,可以使用以下代码在PHP中接收它

<?php $handle = fopen('php://input','r');
                $jsonInput = fgets($handle);
                // Decoding JSON into an Array
                $decoded = json_decode($jsonInput,true);
?>
Run Code Online (Sandbox Code Playgroud)