Php 如何从iphone post请求中获取json数据

Php 如何从iphone post请求中获取json数据,php,iphone,mysql,cocoa-touch,json,Php,Iphone,Mysql,Cocoa Touch,Json,我正在使用JSON框架开发一个iPhone应用程序,我正在调用一个PHP脚本来更新本地服务器上的MySQL数据库。使用这些代码: NSString *jsonString = [sendData JSONRepresentation]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; NSString*post = [NSString stringWithFormat:@"&json=%@", json

我正在使用JSON框架开发一个iPhone应用程序,我正在调用一个PHP脚本来更新本地服务器上的MySQL数据库。使用这些代码:

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

[request setURL:[NSURL URLWithString:@"http://localhost:8888/update.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:postData];
我想问:

从该请求中获取数据的PHP脚本是什么,以及如何将JSON数据解码为PHP对象以更新到数据库


如果有任何帮助,我们将不胜感激。

我还没有在iPhone上这样做,但看起来它只是:

if(isset($_REQUEST['json']) && $_REQUEST['json']) {
    $jsonObj = json_decode($_REQUEST['json']);
    //mandatory sanitizing and verification here
    //PDO examples
    //$stmt = $db->prepare('INSERT ...');
    //$stmt->execute(array($jsonObj->userId, $jsonObj->specialData));
    //check statement execution
}
更多信息:


以下是您提供的有限信息的几行代码

<?php
// get data
$rawJsonData = $_POST;
$decodedData = json_decode($rawJsonData);

// .. do some parsing on $decodedData ..

// save data
$db = new Db();
$db->insert('table', $decodedData);

您将在变量$\u POST['json']中找到您的数据,您可以通过以下方式查看您通过POST收到的信息:

<?php print_r($_POST); ?>

一旦确定了数据的位置,就可以使用以下方法将JSON数据传递到PHP数据:

<?php $phpObj = json_decode($_POST['json']); ?>

同样,您可以使用print\r查看数据的结构:

<?php print_r($phpObj); ?>