2013-02-09 166 views
2

我創建了一個Web服務來在iOS應用程序和Joomla網站之間進行通信,並且我使用GET方法在移動應用程序和Web服務之間進行通信, web服務和控制器(做文件並返回數據的PHP文件),但我沒有找到如何將實現轉換爲POST方法,這裏是實際系統:將Web服務從GET轉換爲POST

ws.php:它是web服務(簡單示例)

<?php 
     $id = $_GET['id'] ; // get the data from the URL 

// here i make testes 

// then I redirect to the controller of the Joomla component that receive 
// the call of the request the URL is actually the attribute "action" of 
// an existing HTML Form that implement the login system, I added a 
// parameter called web service to help me to modify the controller 
// to make the difference between a normal call and a web service call 

header("Location: index.php?option=com_comprofiler&task=login&ws=1&id=1"); 
?> 

Controller.php這樣:Web服務調用的接收器和網絡電話(從瀏覽器)

<?php 
// code added by me, in the existent controller of the component 
// it was waiting for a form submitting, so I got to convert my data to POST here 

if (isset($_GET['ws'])) // it's a web service call 
{ 
    $_POST['id'] = $_GET['id'] ; 

    // do the task ... 

    if ($correctLogin) // just an example 
    echo "1" 
    else 
    echo '0'; 
} 
?> 

我沒有把真正的實現,這是該系統的只是一個簡單的例子,但它是相同的從移動

NSURL *url = [[NSURL alloc]initWithString:@"http://localhost/ws.php?id=1"]; 

NSData *dataUrl= [NSData dataWithContentsOfURL:url]; 

NSString *str = [[NSString alloc]initWithData:dataUrl 
             encoding:NSUTF8StringEncoding]; 

if(![str isEqualToString:@"0"]) 
    NSLog(@"connected"); 
else 
    NSLog(@"not connected"); 

所以

呼叫我不想使用GET方法,我只是想從使用POST手機收到我的數據,並使用這些數據發送到控制器POST也什麼是最好的解決方案?

+1

你就不能使用'$ _REQUEST',因爲它可以同時處理'$ _POST'和'$ _GET'? – j08691 2013-02-09 20:31:18

+0

我不想在開始時使用'$ _GET',我只是想使用'$ _POST'從移動設備發送數據,並且從web服務發送數據給控制器。在實際系統中,我使用重定向從兩個PHP文件發送數據,但是我想更改它並使用'$ _POST'在兩個PHP文件之間以及移動應用程序和Web服務之間發送數據。 – Aladin 2013-02-09 20:38:49

回答

1

如果您希望您的應用使用POST方法發送數據,那麼我就是這個代碼。我希望這會有所幫助。

它需要在字典對象中發送數據。 Ecodes所述要發送的數據作爲POST ,然後返回響應(如果你想以字符串格式的結果,可以用[[的NSString的alloc] initWithData:dresponse編碼:NSASCIIStringEncoding];返回數據時)

-(NSData*) getData:(NSDictionary *) postDataDic{ 

    NSData *dresponse = [[NSData alloc] init]; 

    NSURL *nurl = [NSURL URLWithString:url]; 
    NSDictionary *postDict = [[NSDictionary alloc] initWithDictionary:postDataDic]; 
    NSData *postData = [self encodeDictionary:postDict]; 

    // Create the request 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nurl]; 
    [request setHTTPMethod:@"POST"]; // define the method type 
    [request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

     // Peform the request 
     NSURLResponse *response; 
     NSError *error = nil; 

     dresponse = [NSURLConnection sendSynchronousRequest:request 
                returningResponse:&response 
                   error:&error]; 

    return dresponse; 
} 

這種方法準備字典數據POST

- (NSData*)encodeDictionary:(NSDictionary*)dictionary { 
    NSMutableArray *parts = [[NSMutableArray alloc] init]; 
    for (NSString *key in dictionary) { 
     NSString *encodedValue = [[dictionary objectForKey:key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue]; 
     [parts addObject:part]; 
    } 
    NSString *encodedDictionary = [parts componentsJoinedByString:@"&"]; 
    return [encodedDictionary dataUsingEncoding:NSUTF8StringEncoding]; 
} 
+0

謝謝,這很有用,但只有在iOS實現方面,我仍然需要將Web服務從使用GET更改爲POST。 – Aladin 2013-02-12 10:34:05

+0

您正在重定向到其他網址,因此您無法通過url發送發佈數據,您可以使用curl來獲取結果,但另一個解決方案可以將您的POST數據轉儲到會話變量中,如($ _SESSION ['post'] = $ _POST;)然後在你的控制器檢查$ _SESSION ['post']是否有數據,如果沒有數據正常行爲,否則$ _POST = $ _SESSION ['post'];和未設置($ _ SESSION ['post']); – MTahir 2013-02-13 22:55:14

+0

再次感謝您的評論,因爲我知道session變量是客戶端,因爲如果我沒有錯誤,它將數據存儲在cookie中,並且我的請求將來自移動設備,而不是來自瀏覽器!糾正我,如果我錯了?但cURL的東西我認爲它可以工作,基本上我需要提交/發送一個存在的HTML表單的動作屬性的數據 – Aladin 2013-02-15 23:02:01