Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send some arguments to a PHP using the iPhone

Tags:

php

iphone

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?

like image 825
isiaatz Avatar asked Jan 24 '23 05:01

isiaatz


2 Answers

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.
like image 87
pix0r Avatar answered Jan 31 '23 17:01

pix0r


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

like image 27
Eli Avatar answered Jan 31 '23 19:01

Eli