简体   繁体   English

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

[英]How can I receive an HTTP response from my PHP script?

I've successfully sent a POST request to my server but I want to know how to send a response from PHP without using JSON objects or the database. 我已经成功向服务器发送了POST请求,但是我想知道如何在不使用JSON对象或数据库的情况下从PHP发送响应。 I was thinking I could always send the database row values for the particular entry to my application and then compare them to see if the values match, but that seems a little verbose and undesirable. 我以为我总是可以将特定条目的数据库行值发送到我的应用程序,然后将它们进行比较以查看这些值是否匹配,但这似乎有些冗长和不受欢迎。 I'd much rather send an HTTP POST response from my PHP script, if the log is correct which would contain a boolean variable depending on whether the encrypted passwords matched. 如果日志是正确的,我宁愿从我的PHP脚本发送HTTP POST响应,该日志将包含一个布尔变量,具体取决于加密密码是否匹配。 I've looked all over the place but I can only find information about sending HTTP POST requests from the application, not receiving them from a PHP script on a server. 我到处都是,但是我只能找到有关从应用程序发送HTTP POST请求的信息,而不能从服务器上的PHP脚本接收这些信息。

Edit: Here is an IBAction which I'm using to open the connection to send data with POST. 编辑:这是一个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 :|");
}

If you want to use the delegate way (you're currently using), make sure your class implement the delegate 如果要使用委托方式(当前正在使用),请确保您的类实现委托

@interface MyViewController : UIViewController <NSURLConnectionDataDelegate>

and you can use following delegate method to get the data 您可以使用以下委托方法获取数据

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

Another example is using the sendAsynchronousRequest method, simply replace your registrationConnection 另一个例子是使用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  
                           }];

I've figured out a way to return a value from an associative array in my PHP script. 我想出了一种从PHP脚本中的关联数组返回值方法。 I was basically asking if there were a better way to receive this data (id est by sending an HTTP POST request from my PHP script to my application which would then store the value of that POST request as a variable in the application). 我基本上是在问是否有更好的方法来接收此数据(例如,通过从我的PHP脚本向应用程序发送HTTP POST请求,然后将该POST请求的值存储为应用程序中的变量)。 This is what I've done to return information from the PHP request to the application. 这就是我将信息从PHP请求返回到应用程序所要做的。 It uses JSON which I wanted to avoid, but I'm guessing this is the only (or at least, the mosts beginner-friendly way) 它使用了我想避免的JSON,但是我猜想这是唯一的(或者至少是最适合初学者的方式)

In my iOS application I've added a button to the storyboard which will trigger an IBAction which will send a POST request to my PHP document which I have on my server. 在我的iOS应用程序中,我向情节IBAction板上添加了一个按钮,该按钮将触发IBAction ,该IBAction将向我的服务器上的PHP文档发送POST请求。 This line is near the end of that PHP document: echo json_encode(array('registrationSuccessful'=>'true')) , which creates an associative array then converts it to JSON syntax and returns that to the requesting HTTP client (my iOS application). 这行代码接近该PHP文档的结尾: echo json_encode(array('registrationSuccessful'=>'true')) ,它将创建一个关联数组,然后将其转换为JSON语法,并将其返回给发出请求的HTTP客户端(我的iOS应用程序) )。 That means I can use NSJSONSerialization to convert the returned JSON data to an NSArray and use the values of that NSArray within the application to do (or not to do) other things in the application. 这意味着我可以使用NSJSONSerialization返回的JSON数据转换成NSArray并使用该值NSArray在应用程序中的应用程序中做(或不做),其他的事情。

I feel like this won't make much sense, so ask a question if you'd like a more in-depth explanation. 我觉得这没有什么意义,所以请问一个问题,是否需要更深入的解释。 I'd be happy to provide one not only for other guests but also for myself because I'm more than likely going to forget how to do this in a few months. 我很乐意为其他客人提供一个服务,也为我自己提供服务,因为我很可能会忘记几个月后的操作方式。 :( :(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取Swift代码片段以从PHP脚本接收返回的值? - How do I get this snippet of Swift code to receive the returned values from PHP script? iOS:我如何接收HTTP 401而不是-1012 NSURLErrorUserCancelledAuthentication - iOS: How can i receive HTTP 401 instead of -1012 NSURLErrorUserCancelledAuthentication 如何从TableViewController接收结果? - How can I receive results from TableViewController? 如何在Swift中查看来自HTTP GET请求的服务器响应? - How can I see the server response from an HTTP GET request in Swift? 如何从NetworkingService响应访问HomeViewController内部的User属性? - How can I access the User properties inside my HomeViewController from my NetworkingService response? 无法从我的php接收推送通知消息,但是在线测试站点推送可以获取该消息 - Can't receive the push notification message from my php, but the online test site push is can get the message 如何将Restkit 0.10.0中的loadObjectsAtResourcePath用于php脚本的http发布? - How to use loadObjectsAtResourcePath from Restkit 0.10.0 for a http post to a php script? 如何保存响应来自我的服务器,以及如何访问该数据 - How to save the response From My server , and How can i access That data 我可以在AppDelegate中收到摇动事件吗? - Can I receive shake events in my AppDelegate? 我怎样才能部分地从Api接收数据? - how can I receive data from Api by parts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM