1

What is the best way to pass data input from an iphone app to a php file?

user559142
  • 12,279
  • 49
  • 116
  • 179
  • A PHP file? I guess you mean a PHP application running on a webserver? You should probably clarify your question. You'll probably get a lot of answers, too, since there is no clear "best way". – uvesten Jun 27 '11 at 16:13
  • A fair assumption, but the PHP file is located on an external, web-accessible server - correct? – Jason McCreary Jun 27 '11 at 16:13
  • 2
    `HTTP`, unless you want to roll your own protocol. – Marc B Jun 27 '11 at 16:14

3 Answers3

2

A NSURLRequest POST or GET call to the php file.

- (void) <functionname>:<parameters> {   
    NSString *urlString = [NSString stringWithFormat:@"<url with GET>",<parameters>];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];  NSError * e; 
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
}
looneydoodle
  • 369
  • 6
  • 26
0

If you're asking about posting the data to a web server, I'd recommend using ASIHTTPRequest to HTTP POST the data to the server in Json format. You'd then use the PHP function json_decode() to decode the data.

Json-framework is a good Json framework for the iPhone.

uvesten
  • 3,365
  • 2
  • 27
  • 40
0

I like to simply make a URLRequest, set its method to @"POST", and using a simple xml just send it as the body of the request. Finally with a NSURLConnection and its delegate methods you verify and add more actions depending on the method being called at a given time.

With the connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate method you can handle a response from your php side.

But if all you are doing is receiving something like an XML, you can just make the request to the url and send it to a parser; in this case I usually use NSXMLParser.

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
A Salcedo
  • 6,378
  • 8
  • 31
  • 42