是否有可能在iPhone应用程序中实时发送数据到php应用程序?

时间:2012-02-21 05:14:41

标签: php iphone

我想创建一个将一些数据发送到PHP网站的iPhone应用程序,我知道它可以通过Web服务完成,但是如何?可以实时完成吗?如果我有几部iPhone,我怎样才能制作我的应用程序?

4 个答案:

答案 0 :(得分:1)

 NSString *content = @"message=Message&user=21";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com/form.php"]];//The address of the website page
[urlRequest setHTTPMethod:@"POST"]; //Method GET/POST. POST is recommended
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];


// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];

来自:How to write data to the web server from iphone application?

答案 1 :(得分:1)

您可以使用json格式的数据作为webservice调用发送到php应用程序。 Php可以选择从json格式获取数据。另外php应用程序可以以json格式返回响应,可以由iphone处理。

答案 2 :(得分:0)

您可以使用链接

将数据作为查询字符串传递

e.g。 www.yoursite.com/option/update?id =

以HttpRequest发送

答案 3 :(得分:0)

在iOS中实现此目标的典型方法是通过RESTful服务。许多网站通过GET查询公开他们的数据。任何结果都以机器可读格式返回,例如JSON或XML(而不是标准HTML)。

例如,Twitter有一个RESTful API

举个例子,试试http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed

这将返回搜索带有短语“blue angels。”的推文。

假设您想通过PHP访问自己的网站,您需要创建自己的API(以RESTful格式返回结果的网页)。

Here是一篇博客教程,介绍了通过iOS使用RESTful服务的过程。