简体   繁体   中英

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. 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. 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.

Edit: Here is an IBAction which I'm using to open the connection to send data with 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

[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. 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). This is what I've done to return information from the PHP request to the application. It uses JSON which I wanted to avoid, but I'm guessing this is the only (or at least, the mosts beginner-friendly way)

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. 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). 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.

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. :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM