如何从PHP脚本接收HTTP响应?

时间:2014-12-18 04:24:29

标签: ios objective-c http post

我已成功向我的服务器发送 POST 请求但我想知道如何在不使用 JSON 对象的情况下从PHP发送回复数据库。我以为我总是可以将特定条目的数据库行值发送到我的应用程序,然后比较它们以查看值是否匹配,但这看起来有点冗长且不合需要。我更愿意从我的PHP脚本发送一个HTTP POST响应,如果日志是正确的,它将包含一个布尔变量,具体取决于加密的密码是否匹配。我到处都看了,但我只能找到有关从应用程序发送HTTP POST请求的信息,而不是从服务器上的PHP脚本接收它们。

编辑:这是IBAction,我用它打开连接以通过POST发送数据。

NSString *registrationPOSTMessage = [NSString stringWithFormat:@"username=%@&password=%@&emailAddress=%@",self.registrationFieldUsername.text,self.registrationFieldPassword.text,self.registrationFieldEmail.text];
NSData *ASCIIEncodedRegistrationPOSTData = [registrationPOSTMessage dataUsingEncoding:NSASCIIStringEncoding];
NSString *ASCIIEncodedRegistrationPOSTDataLength = [NSString stringWithFormat:@"%ul",(unsigned)[ASCIIEncodedRegistrationPOSTData length]];
NSMutableURLRequest *registrationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.hausofalexander.tk/someiOSApplication/Register.php"]];
[registrationRequest setHTTPMethod:@"POST"];
[registrationRequest setHTTPBody:ASCIIEncodedRegistrationPOSTData];
[registrationRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[registrationRequest setValue:ASCIIEncodedRegistrationPOSTDataLength forHTTPHeaderField:@"Content-Length"];
NSURLConnection *registrationConnection = [[NSURLConnection alloc] initWithRequest:registrationRequest delegate:self];
if (registrationConnection) {
    NSLog(@"The registration information has been sent! :D");
} else if (!registrationConnection) {
    NSLog(@"For some reason the registration information couldn't be sent. :(");
} else {
    NSLog(@"Something horrible has happened :|");
}

2 个答案:

答案 0 :(得分:0)

如果您想使用委托方式(您目前正在使用),请确保您的班级实施委托

@interface MyViewController : UIViewController <NSURLConnectionDataDelegate>

您可以使用以下委托方法获取数据

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    NSLog(@"Did Receive Data %@", data);
}

另一个例子是使用sendAsynchronousRequest方法,只需替换registrationConnection

[NSURLConnection sendAsynchronousRequest:registrationRequest queue:[[NSOperationQueue alloc] init]
                 completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError) {
                            NSLog(@"Response %@", response);
                            NSLog(@"Data %@", data); //Add a breakpoint here to find out whats the  
                           }];

答案 1 :(得分:0)

我已经找到了一种方法来从我的PHP脚本中的关联数组中返回一个值。我基本上问是否有更好的方法来接收这些数据(通过从我的PHP脚本向我的应用程序发送HTTP POST请求,然后将该POST请求的值作为变量存储在应用程序中)。这就是我将PHP请求中的信息返回给应用程序所做的工作。它使用我想避免使用的JSON,但我猜这是唯一的(或者至少是最适合初学者的方式)

在我的iOS应用程序中,我在故事板上添加了一个按钮,该按钮将触发IBAction,它将向我的PHP文档发送POST请求,该文档位于我的服务器上。该行接近该PHP文档的末尾:echo json_encode(array('registrationSuccessful'=>'true')),它创建一个关联数组,然后将其转换为JSON语法,并将其返回给请求的HTTP客户端(我的iOS应用程序)。这意味着我可以使用NSJSONSerialization将返回的JSON数据转换为NSArray,并使用应用程序中NSArray的值来执行(或不执行)应用程序中的其他操作

我觉得这没有多大意义,所以如果你想要更深入的解释,可以问一个问题。我很乐意不仅为其他客人提供一个,也为我自己提供一个,因为我很可能会在几个月内忘记如何做到这一点。 :(