Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send iPhone HTTP request to Apache PHP webserver

I am programmer with a games and 3D graphics background and at the moment I would like to brush up on my networking and web development skills.

I have a task that I would like to accomplish to do this. My idea is that I would like to be able to send a HTTP request or something similar to my webserver, which runs a LAMP based setup. I would like to know how I can send a HTTP request containing some information from my iPhone using the Cocoa Touch framework, to my webserver.

I would like the webserver (using PHP) to be able to record the sent information into a plain text file, that I can use later to make graphs. For my example we could just send the current date.

I think that people must do this very often and I really want to know how to do this. Thanks for your help.

P.S. If you don't know the Cocoa code in order to send the request, that's okay I'm sure I can figure that out, but I would like to at least know how to get the Linux server to save the HTTP request, preferrably PHP but another appropriate language is fine. Bonus marks for away to do this securely.

Also: I am a total noob at this and require source code, cheers :D

like image 816
Brock Woolf Avatar asked Jul 25 '09 09:07

Brock Woolf


2 Answers

You really can't go past a library like ASIHTTPRequest if you need to make HTTP requests from an iPhone or Mac client to a web server. The documentation is very good, and covers all the important topics like:

  • asynchronous vs synchronous requests
  • sending data to the server
  • tracking upload/download progress
  • handling authentication
  • using the keychain for storage of credentials
  • compressing request bodies with gzip

You should check it out -- it will make your life much easier.

like image 137
Nathan de Vries Avatar answered Oct 14 '22 14:10

Nathan de Vries


Okay no one has given an answer so I went off and discovered a project that details how to do this using either a Mac native app for a client or a PHP web page client. I modified some of the original server code slightly, just so you know I have tested this on my own site and it works for uploading files to a web server.

PHP Server (uploader.php)

<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name'] );
$filename = "\"" . basename( $_FILES['uploaded']['name'] ) . "\"";
$ok = 1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
   echo "$filename";
   echo "was uploaded successfully";
}
else 
{
   echo "$filename";
   echo "upload failed";
}
?>

Web Client (index.php)

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Cocoa Client

The code for the Cocoa client is quite long and the code that goes with the above can be found here. There is an alternative here, which I think is better code, but I haven't tested it with the PHP server above. I expect you could modify it with minimal changes if any though.

Here is a basic way in Cocoa to send a text POST request to a webserver:

NSString* content = [@"item=" stringByAppendingString:@"Something to
Post"];

  NSURL* url = [NSURL URLWithString:@"http://www.mysite.com/index.php"];
  NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
  [urlRequest setHTTPMethod:@"POST"];
  [urlRequest setHTTPBody:[content dataUsingEncoding:NSASCIIStringEncoding]];

I like this because it is a minimal solution and you can build upon it easily and hopefully this can help someone else should they come across this :)

like image 23
Brock Woolf Avatar answered Oct 14 '22 14:10

Brock Woolf