PHPページを介してiOSアプリからmySQLデータベースにJSONデータを送信しようとしています。何らかの理由で、私の POST データが php ページで利用できません。
- (IBAction)jsonSet:(id)sender {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"firstvalue", @"firstkey", @"secondvalue", @"secondkey", nil];
NSData *result =[NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSURL *url = [NSURL URLWithString:@"http://shred444.com/testpost.php"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", jsonRequestData.length] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonRequestData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
phpページが呼び出され、データベースへの書き込みが確認されていることはわかっていますが、
私のphpファイルの最初の数行は、POSTデータを取得します
<?php
// Put parameters into local variables
$email = $_POST["firstkey"];
...
しかし、何らかの理由で、$email も空の文字列です。APIkitchen.com を使用してページをテストし、それが機能することを確認できるため (Content-type フィールドと Content-Length フィールドを除外した場合のみ)、問題は iOS コードにあると感じています。