I’m needing to use an HTTP request from an iPhone to a PHP script for an app I’m building. The PHP script returns a string of text. I’ve seen a lot of people talking about ASIHTTPRequest but it doesn’t work under iOS5. I’ve been looking at the Apple Developer documentation but I don’t really understand how to use NSURLRequest. This is what I have at the moment:
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
[req setURL:[NSURL URLWithString:@"http://location.of.my.php.script"]];
[req setHTTPMethod:@"GET"];
[req setValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:postData];
Basically what I want to do is do the HTTP request, send an example URL: “http://hellothere.com/myscript.php?options=Type&type=foo then, in my PHP script have:
if($_GET[‘option’] == ‘Type’) {
//do some stuff and return a string
}
Can anyone point me in the right direction as to how to use NSMutableURLRequest or NSURLConnection? Or can someone point me to a very basic tutorial?
Thanks
I am using ASIHTTPRequest like this on iOS 5:
You need to implement the ASIHTTPRequestDelegate methods. Make the request like this:
And then the delegates where you unpack the response. This assumes the response is in JSON.