2014-02-17 2 views
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는 내 앱에서 요청을 받지만 내가 보낸 콘텐츠를받지 못하는 문제가 있습니다.

+1

is_array ($ post_data) 대신 is_array ($ obj)가 아니어야합니까? 또한 "original_request"=> $ obj –

+2

또한 json_decode 배열에서 디코딩 할 순서에 의해 두 번째 true 매개 변수가 필요하다고 생각합니다 –

답변

3

왜 매개 변수가 여러 개인 경우 json_decode()을 php : // 입력에 직접 사용하고 있습니까?

이 시도 :

// See the underlying structure of your data; attempt to decode the JSON: 
var_dump($_POST); 

$data = json_decode($_POST['question_title'], true); 
var_dump($data); 

$data = json_decode($_POST['question_body'], true); 
var_dump($data); 

PHP는 우리에게 좋은 이유에 대한 $_POST 자동 전역을 제공합니다. 또한 json_decode($string, true)은 연관 배열을 반환합니다.