Send iPhone HTTP request to Apache PHP webserver

2020-02-17 08:35发布

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

3条回答
甜甜的少女心
2楼-- · 2020-02-17 08:45

Not really a server side guy, but sending the HTTP request can be accomplished in a couple of ways. The most trivially easy is to use +NSString stringWithContentsOfURL: and use GET params in the URL. That will hit the server at the URL you specify, and the GET params can contain the data. The return value can be ignored, and should probably be minimal.

Not going to look it up in depth right now, but I believe you'd use NSStream and NSURLRequest to construct more complex queries, using POST for example.

查看更多
叛逆
3楼-- · 2020-02-17 09:00

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 :)

查看更多
老娘就宠你
4楼-- · 2020-02-17 09:02

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.

查看更多
登录 后发表回答