2015-11-27 49 views
2

試圖向遠程PHP文件發出簡單的發佈請求。我沒有收到服務器上的參數。我使用Postman Btw收到的。所以這項服務似乎沒問題。有些東西似乎缺少iOS代碼。發佈請求未從iOS設備到達服務器

- (void) sendPostRequest { 
NSError *error; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
NSURL *url = [NSURL URLWithString:baseURLPost]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPMethod:@"POST"]; 
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: 
         @"JSON", @"responseType", 
         [UserManager sharedInstance].user.username, @"username", 
          self.textView.text, @"text", 
         nil]; 

NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error]; 
allowLossyConversion:YES]; 
[request setHTTPBody:postData]; 
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    if (error) { 
     NSLog(@"%@", [error.userInfo valueForKey:@"NSDebugDescription"]); 
    } else { 
     NSArray *jsonResponseArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; 
     NSLog(@"%@", jsonResponseArray); 
    } 
}] resume]; 
} 

我的PHP代碼來接收post REquest。

<?php 
header("Content-type:application/json"); 

$postText= $_POST['text']; 
$username = $_POST['username']; 
$itemType = $_POST['itemType']; 
$responseType = $_POST['responseType']; 
$pictureLink = $_POST['pictureLink']; 
$timestamp = $_POST['timestamp']; 
$tags = $_POST['tags']; 

if($timestamp==null){ 
    $timestamp = date('Y-m-d G:i:s'); 
} 
$arr = array('username' => $username); 
echo json_encode($arr); 
?> 

從這裏jsonResponseArrayusername = "<null>";

回答

1

我終於在斯威夫特重新寫全的iOS類的東西又開始滾動。 PHP服務已經有一段時間了,過去對我來說工作得很好。

2

使用

$handle = fopen('php://input','r'); 
$jsonInput = fgets($handle); 

,而不是$_POST

像這樣:

<?php 
    header("Content-type:application/json"); 

$handle = fopen('php://input','r'); 
$jsonInput = fgets($handle); 
$arr = json_decode($jsonInput, true); 

$postText= $arr['text']; 
$username = $arr['username']; 
$itemType = $arr['itemType']; 
$responseType = $arr['responseType']; 
$pictureLink = $arr['pictureLink']; 
$timestamp = $arr['timestamp']; 
$tags = $arr['tags']; 

if($timestamp==null){ 
    $timestamp = date('Y-m-d G:i:s'); 
} 
$arr = array('username' => $username); 
echo json_encode($arr); 
?> 
+0

感謝您對php代碼的所有幫助。我仍然想保持這個問題幾天,我的PHP代碼是非常古老的,並在過去爲我工作。我希望有人會用我的obj-c來幫助我一些,而且我不會那麼改變我的php代碼。 – CalZone

+0

@BabakT你可以解釋爲什麼這個工程,通過使用$ _POST?我有同樣的問題,使用來自objC應用程序的POST請求將JSON發送到服務器,但我只能使用php:// input fopen調用訪問JSON。我很好奇爲什麼。 – Tiago

0

不正確的答案,但是這個代碼爲我工作:

//Dict -> your dictionary, url -> the url for the action 

NSData *data = [NSJSONSerialization dataWithJSONObject:dict 
               options:0 
               error:&error]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
[request setURL:url]; 

NSString *postLength=[NSString stringWithFormat:@"%lu",(unsigned long)[data length]]; 

[request setHTTPMethod:@"POST"]; 

[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setTimeoutInterval:20.0]; 
[request setHTTPBody:data]; 

//.... 
//rest of the code 
+0

這裏和我的代碼有什麼不同? – CalZone

+0

似乎只有postLenth是不同的。 – CalZone

+0

這個確切的代碼已經爲我工作了很長一段時間的PHP服務器。這可能會有所幫助。 – ShahiM

相關問題