Php 我有一个网站和一个iOS应用程序。我将如何为他们构建一个API?
我有一个网站和一个本地iOS应用程序。现在,该应用程序通过下拉一个XML文件与该站点进行通信。我很好奇我如何能制作一个API,这样他们就可以通过现场编程的方式进行对话。这将允许我用这个应用做更高级的事情 我以前从未做过这样的事情,我不知道从哪里开始,有什么建议吗 该网站是用PHP btw编写的,这并不重要,因为我想制作的API将与现有代码分离Php 我有一个网站和一个iOS应用程序。我将如何为他们构建一个API?,php,ios,Php,Ios,我有一个网站和一个本地iOS应用程序。现在,该应用程序通过下拉一个XML文件与该站点进行通信。我很好奇我如何能制作一个API,这样他们就可以通过现场编程的方式进行对话。这将允许我用这个应用做更高级的事情 我以前从未做过这样的事情,我不知道从哪里开始,有什么建议吗 该网站是用PHP btw编写的,这并不重要,因为我想制作的API将与现有代码分离 -谢谢我同意使用REST API是一种很好的方法 如果您打算在没有RESTAPI的帮助下编写自己的web服务,那么查看与web服务器的交互可能会很有说明性
-谢谢我同意使用REST API是一种很好的方法 如果您打算在没有RESTAPI的帮助下编写自己的web服务,那么查看与web服务器的交互可能会很有说明性。如果您能够了解这种方法,那么就可以使用RESTAPI解决这个问题,也许可以更好地理解幕后发生的事情(并欣赏API带来的成果) 例如,下面是一些普通的PHP,它从iOS设备接收JSON输入,格式如下:
{"animal":"dog"}
它将返回JSON,指示该动物将发出的声音:
{"status":"ok","code":0,"sound":"woof"}
(其中“状态
”是指请求是“正常
”还是“错误
”,其中“代码
”是一个数字代码,用于识别错误类型(如果有),如果请求成功,“声音
”是该动物发出的声音。)
这个简单示例的PHP源代码,animal.PHP
,可能如下所示:
<?php
// get the json raw data
$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
$http_raw_post_data .= fread($handle, 8192);
}
fclose($handle);
// convert it to a php array
$json_data = json_decode($http_raw_post_data, true);
// now look at the data
if (is_array($json_data))
{
$animal = $json_data["animal"];
if ($animal == "dog")
$response = array("status" => "ok", "code" => 0, "sound" => "woof");
else if ($animal == "cat")
$response = array("status" => "ok", "code" => 0, "sound" => "meow");
else
$response = array("status" => "error", "code" => 1, "message" => "unknown animal type");
}
else
{
$response = array("status" => "error", "code" => -1, "message" => "request was not valid json");
}
echo json_encode($response);
?>
- (IBAction)didTouchUpInsideSubmitButton:(id)sender
{
NSError *error;
// build a dictionary, grabbing the animal type from a text field, for example
NSDictionary *dictionary = @{@"animal" : self.animalType};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
if (error)
{
NSLog(@"%s: error: %@", __FUNCTION__, error);
return;
}
// now create the NSURLRequest
NSURL *url = [NSURL URLWithString:@"http://insert.your.url.here.com/animal.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
// now send the request
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// now parse the results
// if some generic NSURLConnection error, report that and quit
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
if (error)
{
NSLog(@"%s: NSURLConnection error=%@", __FUNCTION__, error);
return;
}
// otherwise, we'll assume we have a good response, so let's parse it
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
// if we had an error parsing the results, let's report that fact and quit
if (error)
{
NSLog(@"%s: JSONObjectWithData error=%@", __FUNCTION__, error);
return;
}
// otherwise, let's interpret the parsed json response
NSString *status = results[@"status"];
if ([status isEqualToString:@"ok"])
{
// if ok, grab the "sound" that animal makes and report it
NSString *result = results[@"sound"];
dispatch_async(dispatch_get_main_queue(),^{
self.label.text = result;
});
}
else
{
// if not ok, let's report what the error was
NSString *message = results[@"message"];
dispatch_async(dispatch_get_main_queue(),^{
self.label.text = message;
});
}
}];
}
显然,这是一个微不足道的例子(更有可能是PHP服务器在服务器上的数据库中存储或查找数据),但更完整的PHP web服务超出了这个特定于iOS的问题的范围。但希望这能让您了解iOS应用程序与基于PHP的web服务交互的一些构建块(设计web服务接口,编写PHP以支持该接口,编写iOS代码以与该web服务接口交互)。提取xml不是一种活的编程方式吗?XML是动态生成的吗?最好的方法是构建一个REST API,REST API允许通过XML或JSON进行双向通信。你是老板。这正是我想知道的。非常感谢你这么做。