Using gzcompress in PHP; need to be able to uncompress on iPhone - iphone

I'm sure this isn't too difficult (and I'm surprised I can't figure it out), but here goes:
So. I'm using gzcompress() to compress JSON data in PHP so I can send it into an iPhone app.
Having some troubles figuring out how to uncompress this data on the iPhone (iOS).
Any thoughts? I'm grabbing the data via a NSMutableURLRequest.
Thanks!
Code making the request:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Code processing the response:
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
And on the PHP side:
echo(gzcompress(json_encode($lines)));
Thanks!!!

This article suggests that NSMutableURLRequest supports gzip decompression out-of-the-box, but you need to add an Accept-Encoding: gzip header to the request.
Extending your example:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[request setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
I'd also recommend testing the PHP script with cURL to make sure it's sending valid gzipped data.

Related

How to store the values in web server from i phone application

I am developing view based application.In my view i have register page in that page i have
some fields like Firstname, lastname,e mail ID. When we click save button after entering the values these all field should be store in in webserver.In webserver i have application that application was developed using .net MVC Architecture and database is MYSQL .How i can store these values in webserver.
thanks for your response i have written this code is it right way to store values in webserver
-(IBAction)buttonClick:(id)sender
{
NSString* firstname = nameInput.text;
NSString* lastname = passInput.text;
NSString* bname = lastInput.text;
NSString *post =
[[NSString alloc] initWithFormat:#"fname=%#&lname=%#&email=%#",firstname,lastname,bname];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php?%#",post]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) NSLog(#"Connection Successful");
}
#end
You have two options:
1) Develop a proper API that your iOS app can call using libraries like ASIHTTPRequest or AFNetworking
2) Have a .NET form processor in place, just like you would to process an HTML form, and then use ASIHTTPRequest, AFNetworking, or something similar to submit the request to this processor using the POST method and with the parameters you would like to store added to the request. This simulates an HTML form that has been filled and submitted, and then you can do whatever you wish with the data .NET receives. Both ASI and AFNetworking have pretty extensive documentation on how these types of requests are implemented on iOS with their respective libraries. Unfortunately, ASI is no longer being maintained, so I would recommend going with AFNetworking if possible.
RESPONSE TO UPDATE:
I only gave it a quick look over, but everything looks good to me. The only thing I would change is this:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php?%#",post]]];
to this:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php"]]];
What you will need now is code on the web side in that file.php that can process the request. It would use the typical $_POST['var_name_here'] to grab the data that is passed to it.
You have to implement a webapplication in your preferred language and deploy it to some webserver or hosting service. To get started, you should choose a language PHP, Java, Python, C# or whatever and google how you could implement a webservice within that language.
I would do this in Java and implement a RESTful wbeservice that sends and receives JSON or XML.

API image file upload iPhone to Ruby on Rails

I am a backend Rails developer of an API that services several iPhone clients. I'm not an iPhone dev.
I have a need to accept binary data (several image files in this case) from the client via a POST request to the API.
To get the file content (file metadata other than image type is not relevant here), what tools might be used by the iPhone developer? I've found ObjectiveResource (used by iPhone on Rails) and ASIHTTPRequest. In the pages I found for those, there's no indication of what form the uploaded file will have when the controller action is executed. Will it be a Ruby File object or Tempfile object? I don't control the iPhone code development, there are some cross-cultural communication difficulties there, and they haven't used those suggestions so far. If I can submit better information to them, I might be getting better data back.
The backend app is currently running Rails 2.3.10, and will soon (in the next few weeks) likely be converted into Rails 3.
Thanks,
Craig
ObjectiveResource does not natively support file uploads. Try instead using ASIHTTPRequest with this snippet:
NSURL *url = [NSURL URLWithString:#"http://localhost:3000/file"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Sample" forKey:#"name"];
[request setFile:... forKey:#"file"];
[request startSynchronous];
For more details, see the example page here (sending data).
The post will be encoded as a standard multipart form post (just like if it came from an HTML form). If you are using paperclip to store your uploads, the magic will just happen!
Use JSON over HTTP
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:urlStr]];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:#"%d", [jsonMessageStr length]];
[request setValue:requestDataLengthString forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPBody:jsonData];
NSURLConnection *theConnection =
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

How can I send request and get a file response in iPhone?

My problem is the following...
I read about sending http requests and receiving their responses on iPhone SDK 3.2 using NSURLRequest and NSHTTPURLResponse (All my requests are "get" and there's no "post") but I don't know how to do that exactly cause some of my responses are just strings (plain text) and some others are binary files (gzip and mp3)
Thank you in advance for the help
To perform a Http request, I use ASIHttpRequest:
NSURL *url = [NSURL URLWithString:my_url];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
[request startSynchronous];
That works perfectly. Still need to figure out if it's fine with mp3 / zip files.
Luc

How to enable cookies when POSTING with URLConnection in iPhone SDK?

I am trying to do a POST towards a site which utilizes secure session with cookies.
Ofcourse this won't work with the code I have posted below. It keeps responding with a non-authorized message.
Is there any way I can use cookies in my code or at least simulate cookie usage?
NSURL *url = [[NSURL alloc] initWithString:#"https://long_and_complicated_url"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Best regards
//Abeansits
The SDK has cookie support in the form of NSHTTPCookie and NSHTTPCookieStorage
Have you familiarized yourself with these?

Can I log in to my site from my iPhone app?

I'm trying to make a log in or sign up feature for my web site in my iPhone app. My website is a content management system, and like any other CMS, it has log in and registration features. It also has permmissions, dependent on the user account. I think I would have to use UIWebView for this.
Are there any examples or tutorials I can examine?
Check out the documentation for NSURLRequest (and NSMutableURLRequest): you can use it to make a POST request to your login and registration pages, just like a web browser. You can write the form UI in Cocoa/Objective-C and then send the data to the server.
As far as displaying the result to the user, you'll have to figure out a way to either parse the returned HTML (bad idea) or modify your CMS to return JSON or XML to iPhone requests (better idea).
Edit: Here's some sample code, taken from an app I'm working on (it submits data to Last.fm using POST):
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *str = #"This is my example data!";
// everything below here is directly from my app:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:kLastFMClientUserAgent forHTTPHeaderField:#"User-Agent"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPShouldHandleCookies:NO];
*connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];