简体   繁体   中英

PHP to Objective C (iPhone App) Communication

I have an iPhone app that posts data to a php app which in turn stores this data in a remote mysql database.

I wanted the php file to inform the iphone app whether or not the storage was successful. Here is my php code:

<?php

//connect to database
function connect() {
  $dbh = mysql_connect ("localhost", "123", "456789") or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db("PDS", $dbh); 
  return $dbh;
}

//store posted data
if(isset($_POST['message'])){
  $message = $_POST['message'];
  $dbh = connect();
  $query = "INSERT INTO  messages (message) VALUES ('$message')";
  $result = mysql_query( $query ) or die ("didn't query");        
}
?>

1) How would I modify the php file above to echo something based on the success/failure of the requested SQL query?

2) What part of the objective C API handles reading in php variables

There is no direct communication between Objective-C and PHP. You query a Web-URL in Objective-C using NSURLConnection or better yet ASIHttp . The queried resource then does something with the parameters you're giving it (in your case, by posting to it) and returns something. Generally, you agree on a standard (say, JSON) to communicate beforehand, or just use HTTP-Statuscodes for this kind of stuff. Than you can exmaine the response given by the NSURLConnection (look at the docs) and find out what happend on the server

Update: ASI-Http has the advantage of being more straight forward to use, it encapsulates a lot of low-level stuff for you. Otherwise, it does the same thing that NSURLConnection does

Next Update: Here's the solution with ASIHTTP and correct String parsing. This uses HTTP Get, put post won't be much more difficult

NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
  NSString *response = [request responseString];
  NSArray *results = [response componentsSeparatedByString:@", "]; 
  for (NSString* result in results) {
   NSString * trimmedResult = [result stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
   if ([trimmedResult isEqualToString:@"failure"]) 
    NSLog(@"operation %i failed.", i + 1);
   }
 }

Just make the php echo directly onto the page and have the iPhone app get the contents of the page and parse them.

eg If the php pages echoes success, failure, success , you could use:

NSString *contents = readthepage (google how to do this)
NSArray *results = [contents componentsSeparatedByString:@", "];
for (int i = 0; i < [results count]; i++) {
    NSString *result = [results ObjectAtIndex:i];
    if ([result rangeOfString:@"failure"].length > 0) // this isn't the perfect test, it checks whether _result_ contains the string @"failure", not is equal to. If it matters to you, find a different method.
        NSLog(@"operation %i failed.", i + 1);
    }
}

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