简体   繁体   English

如何创建RESTful API?

[英]How to create a RESTful API?

Over the last few weeks I've been learning about iOS development, which has naturally led me into the world of APIs. 在过去的几周里,我一直在学习有关iOS开发的知识,这自然使我进入了API领域。 Now, searching around on the Internet, I've come to the conclusion that using the REST architecture is very much recommended, due to its supposed simplicity and ease of implementation. 现在,在Internet上四处搜索,我得出的结论是,由于其结构简单且易于实现,因此强烈建议使用REST体系结构。

However, I'm really struggling with the implementation side of REST. 但是,我真的在REST的实现方面挣扎。 I understand the concept; 我理解这个概念; using HTTP methods as verbs to describe the action of a request and responding with suitable response codes, and so on. 使用HTTP方法作为动词来描述请求的操作并使用适当的响应代码进行响应,等等。 It's just, I don't understand how to code it. 只是,我不知道如何编码。

I don't get how I map a URI to an object. 我不知道如何将URI映射到对象。 I understand that a GET request for domain.com/api/user/address?user_id=999 would return the address of user 999 - but I don't understand where or how that mapping from /user/address to some method that queries a database has taken place. 我了解对domain.com/api/user/address?user_id=999的GET请求将返回用户999的地址-但我不明白从/ user / address到查询a的某些方法的映射在哪里或如何映射数据库已建立。

Is this all coded in one PHP script? 全部都用一个PHP脚本编码了吗? Would I just have a method that grabs the URI like so: 我是否可以像这样获取URI的方法:

$array = explode("/", ltrim(rtrim($_SERVER['REQUEST_URI'], "/"), "/"))

And then cycle through that array, first I would have a request for a "user", so the PHP script would direct my request to the user object and then invoke the address method. 然后遍历该数组,首先我需要一个“用户”的请求,因此PHP脚本会将我的请求定向到用户对象,然后调用address方法。 Is that what actually happens? 那是真的吗?

The main thing I'm not getting is how that URI /user/address?id=999 somehow is broken down and executed - does it actually resolve to code? 我没有得到的主要事情是如何以某种方式分解并执行URI / user / address?id = 999-它实际上解析为代码吗?

class user(id) {
   address() {
     //get user address
   }
}

That's two questions. 这是两个问题。

To honor RESTful HTTP verbs, you have to query $_SERVER["REQUEST_METHOD"] . 要使用RESTful HTTP动词,您必须查询$_SERVER["REQUEST_METHOD"] It will contain the usual GET or POST unless a more specialized HTTP request was received. 除非收到更专门的HTTP请求,否则它将包含常规的GET或POST。 The whole REST buzz is somewhat misleading in itself, and also in misusing the HTTP verbs just for routing. 整个REST嗡嗡声本身在某种程度上具有误导性,而且还误将HTTP动词仅用于路由。

Anyway, the mapping of request URLs to functions can be achieved in two ways. 无论如何,可以通过两种方式实现请求URL到功能的映射。 It's most reliable to use a static map, for example an array that lists URL patterns and destination methods. 使用静态映射(例如列出URL模式和目标方法的数组)是最可靠的。 Most PHP frameworks use an implicit mapping like so: 大多数PHP框架都使用隐式映射,如下所示:

$args = explode("/", trim($_SERVER['REQUEST_URI'], "/"));
$class = array_shift($args);
$method = array_shift($args);
call_user_func_array("$class::$method", $args);

Note how this is a bad example, security-wise. 请注意,从安全角度来看,这是一个不好的例子。 Only allowed and specifically prepared classes and methods should be able to receive requests. 只有允许且经过专门准备的类和方法才能接收请求。 Most frameworks just check if it was derived from an acceptable base class after loading it from a known path and/or instantiating. 大多数框架只是在从已知路径加载和/或实例化之后检查它是否源自可接受的基类。 But a static map is really preferable. 但是,静态地图确实更可取。

Anyway, regular expression mapping or handling by mod_rewrite is also common. 无论如何,通过mod_rewrite进行正则表达式映射或处理也是很常见的。 And for utilizing HTTP verbs, just include it as method name. 对于使用HTTP动词,只需将其作为方法名称即可。

Actually the API you're trying to describe now is not RESTful . 实际上,您现在要描述的API 不是RESTful的 There are many sources describing how to make RESTful APIs. 有许多资料描述如何制作RESTful API。 So you should first define your API (taking in account how your client software would use it) and then implement it. 因此,您应该首先定义您的API(考虑到客户端软件的使用方式),然后实施它。 I'm quite sure that almost any RESTful API can be implemented in PHP. 我非常确定几乎所有RESTful API都可以在PHP中实现。

Here are some other tips on how to make a RESTful API. 以下是有关如何制作RESTful API的其他技巧。

In my opinion GlassFish Server REST Interface is a good example of RESTful design. 我认为GlassFish Server REST接口是RESTful设计的一个很好的例子。

Have a look at FRAPI - http://getfrapi.com/ 看看FRAPI- http ://getfrapi.com/

As it says focus on your business logic, not presentation. 正如它所说的那样,专注于您的业务逻辑,而不是表示。

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

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