0

iPhone SDK からサーバー上の php ファイルに HTTP_POST を呼び出そうとしています。これを以下で呼び出す場合:

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://*****/api2/index.php"]];

[request setHTTPMethod:@"POST"];
[request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];

//create data that will be sent in the post
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@2 forKey:@"value1"];
[dictionary setValue:@"This was sent from ios to server" forKey:@"value2"];

//serialize the dictionary data as json
NSData *data = [[dictionary copy] JSONValue];

[request setHTTPBody:data]; //set the data as the post body
[request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
    NSLog(@"Connection Failed");
}

サーバー上のphpコード

if ($_SERVER['HTTP_METHOD'] == 'postValues'){ 
$body = $_POST; 
$id;// value1 from dictionary 
$name; // value2 from dictionary
}

$id と $name を手伝ってください

4

2 に答える 2

0

追加の値をクエリで送信 (および $_GET を使用して取得) するか、値を json データに配置することができます。

また、json をオブジェクトとして取得する正しい方法は次のとおりです。

$bodyAsObject = json_decode( file_get_contents('php://input') );
于 2013-04-12T08:54:49.667 に答える