0
나는 PHP 파일에 일부 데이터를 보내려고 내가 응답으로 얻을 모든 {"status":"error","code":-1,"original_request":null}
포스트 JSON에 PHP 서버
내 목표 C 코드입니다 : 지금 내 PHP 코드
NSMutableDictionary *questionDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:questionTitleString, @"question_title", questionBodyString, @"question_body", nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://myURL/post.php"]];
[request setHTTPMethod:@"POST"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:questionDictionary options:kNilOptions error:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSURLConnection *postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
그리고 :
$postdata = file_get_contents("php://input");
$obj = json_decode($postdata);
if (is_array($post_data))
$response = array("status" => "ok", "code" => 0, "original request" => $post_data);
else
$response = array("status" => "error", "code" => -1, "original_request" => $obj);
$processed = json_encode($response);
echo $processed;
PHP는 내 앱에서 요청을 받지만 내가 보낸 콘텐츠를받지 못하는 문제가 있습니다.
is_array ($ post_data) 대신 is_array ($ obj)가 아니어야합니까? 또한 "original_request"=> $ obj –
또한 json_decode 배열에서 디코딩 할 순서에 의해 두 번째 true 매개 변수가 필요하다고 생각합니다 –