2013-02-09 117 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