views:

1944

answers:

2

Despite looking at similar posts on this site and google, I just can't wrap my head around how to post to a php page from the iPhone. Here is what I want to do:

I have a php script, say at www.mypage.com/myscript.php that I could post to normally by doing www.mypage.com/myscript.php?mynumber=99&myname=codezy

This in turn will add a log message in database for example. I do not want any data back, it is essentially a one way transaction. NSMutableURLRequest seems to be what I am looking for but I just cant get a handle on how to make this work with a couple parameters.

+5  A: 

If you're sending a single URL request without needing to send multiple variations, NSURLRequest will do. Even though you are using multiple parameters, they are all part of the same URL, so you just treat them that way. Build the URL as a string first and then use the string to initialize a NSURL object.

You are prompting a log message on the server, but you will want to have response data in case something goes wrong. You can just ignore the response data unless there's an error. The request is sent using a NSURLConnection object.

NSURL *urlToSend = [[NSURL alloc] initWithString: @"www.mypage.com/myscript.php?mynumber=99&myname=codezy"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlToSend   
                                            cachePolicy:NSURLRequestReturnCacheDataElseLoad                                
                                   cachetimeoutInterval:30];

NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest  
                                returningResponse:&response 
                                            error:&error];
Pinochle
That looks like exactly what I needed. I will try it @ lunch.
Codezy
Worked great, just had to change cachetimeoutInterval to timeoutInterval. Thanks!
Codezy
A: 

what if I want to send the location of the user?

russell
Codezy