简体   繁体   English

将iPhone HTTP请求发送到Apache PHP Web服务器

[英]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. 我是程序员,拥有游戏和3D图形背景,目前我想了解我的网络和网络开发技巧。

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. 我的想法是,我希望能够发送HTTP请求或类似于我的网络服务器,它运行基于LAMP的设置。 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. 我想知道如何使用Cocoa Touch框架向我的网络服务器发送包含我的iPhone的一些信息的HTTP请求。

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. 我希望网络服务器(使用PHP)能够将发送的信息记录到纯文本文件中,以后我可以使用它来制作图表。 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. 谢谢你的帮助。

PS 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. PS如果您不知道Cocoa代码以发送请求,那没关系我确定我可以解决这个问题,但我想至少知道如何让Linux服务器保存HTTP请求,最好PHP,但另一种合适的语言是好的。 Bonus marks for away to do this securely . 奖励标记为安全地做到这一点。

Also: I am a total noob at this and require source code, cheers :D 另外:我是一个总的菜鸟,需要源代码,欢呼:D

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. 如果您需要从iPhone或Mac客户端向Web服务器发出HTTP请求,那么您真的无法通过像ASIHTTPRequest这样的库。 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 使用gzip压缩请求主体

You should check it out -- it will make your life much easier. 你应该检查一下 - 它会让你的生活更轻松。

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. 好的,没有人给出答案,所以我发现了一个项目,详细说明如何使用Mac本机应用程序为客户端或PHP网页客户端。 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. 我稍微修改了一些原始服务器代码,只是因为你知道我在我自己的网站上测试了它,它适用于将文件上传到Web服务器。

PHP Server (uploader.php) PHP服务器(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) Web客户端(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 . Cocoa客户端的代码很长,上面的代码可以在这里找到。 There is an alternative here, which I think is better code, but I haven't tested it with the PHP server above. 这里有一个替代方案 ,我认为是更好的代码,但我没有使用上面的PHP服务器测试它。 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: 以下是Cocoa向Web服务器发送文本POST请求的基本方法:

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 :) 我喜欢这个,因为它是一个最小的解决方案,您可以轻松地构建它,希望这可以帮助其他人,如果他们遇到这个:)

Not really a server side guy, but sending the HTTP request can be accomplished in a couple of ways. 不是真正的服务器端人,但发送HTTP请求可以通过几种方式完成。 The most trivially easy is to use +NSString stringWithContentsOfURL: and use GET params in the URL. 最简单的方法是使用+ NSString stringWithContentsOfURL:并在URL中使用GET参数。 That will hit the server at the URL you specify, and the GET params can contain the data. 这将在您指定的URL处命中服务器,GET参数可以包含数据。 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. 现在不打算深入研究,但我相信你会使用NSStream和NSURLRequest构建更复杂的查询,例如使用POST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM