tags:

views:

260

answers:

2

Hi I need to send some arguments from the iPhone to a php in the server

PHP:

$text = $_GET['text']);
$mail = $_GET['mail']);
$phone = $_GET['phone']);

iPhone SDK

NSString *text = [[NSString alloc] initWithString:@""]; //very long text
NSString *mail = [[NSString alloc] initWithString:@"[email protected]"];
NSString *phone = [[NSString alloc] initWithString:@"456233876"];

How can I send this strings to the PHP?

+2  A: 

Just use GET or POST data, as normal. It's very simple. Then use an NSURLConnection to send the data, and change the method to POST. Or you can use GET, and just send a URL like http://www.mysite.com/mypage.php?var1=abc&var2=def etc.

See Using NSURLConnection

Eli
+6  A: 

Here's how you'd create an HTTP POST request with NSURLConnection to post the fields "text", "mail", and "phone":

NSURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.myserver.com/page.php"]];
NSMutableString *postBody = [NSMutableString stringWithFormat:@"text=%@", [@"very long text" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[postBody appendFormat:@"&mail=%@", [@"[email protected]" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[postBody appendFormat:@"&phone=%@", [@"1231231234" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[req setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPMethod:@"POST"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];

// Now, implement NSURLConnection delegate methods such as connectionDidFinishLoading: and connection:didFailWithError: to do something with the response from the server.
pix0r