PHP 中的 REST 与 RPC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1098473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST vs. RPC in PHP
提问by the_drow
I'm building my own Ajax website, and I'm contemplating between RESTand RPC.
我正在构建我自己的 Ajax 网站,我正在考虑REST和 RPC。
If my server supported Servlets I'd just install persevereand end the problem, but my server doesn't support Servlets.
如果我的服务器支持 Servlets,我会坚持安装并解决问题,但我的服务器不支持 Servlets。
RPC is simpler to code (IMO) and can be written in PHP easily. All I need is a database query executer. I'm using the Dojo Toolkitand JSON.
RPC 更易于编码 (IMO),并且可以轻松地用 PHP 编写。我只需要一个数据库查询执行器。我正在使用Dojo 工具包和 JSON。
Why should I choose REST over RPC or RPC over REST?
为什么我应该选择 REST over RPC 或 RPC over REST?
采纳答案by back2dos
Uhm ... to put it simple, both are very abstract models ... so abstract, they naturally occur everywhere...
嗯……简单来说,都是很抽象的模型……太抽象了,自然无处不在……
REST is the idea of having resources addressed with a global identifier (the URI in the case of HTTP) that are accessed in a CRUDway (using POST, GET, PUT and DELETE in the case of HTTP ... well, at least that's the idea)...
REST 的理念是让资源使用全局标识符(HTTP 中的 URI)以CRUD方式访问(在 HTTP 中使用POST、GET、 PUT 和 DELETE ......好吧,至少是这样想法)...
RPC is the idea where you call a procedure on a different machine, passing in some parameters, and taking a return value...
RPC 是您在不同机器上调用过程,传入一些参数并获取返回值的想法......
There is a nice short comparison on Wikipedia
Persevere creates a service, that allows both (in a very elegant way, admittedly) ... it is RESTful(although it does not only use HTTP-features to achieve this) andexposes an RPC interface...
Persevere 创建了一个服务,允许两者(以一种非常优雅的方式,诚然)......它是RESTful(虽然它不仅使用 HTTP 特性来实现这一点)并公开了一个 RPC 接口......
In the end, you should look at what your application needs to do ... as most people, you'll probably wind up with an RPC API (be it based on XMLor JSONor whatever), that includes a transport layer for a partially RESTful subsystem ... this is, because having RESTfulnes, means flexibility ... if the client can more or less freely traverse the data on the server (through a set of simple CRUD methods), it does not depend on a limited (problem-specific) set of methods exposed through the API, and you can shift logic clientwards...
最后,你应该看看你的应用程序需要做什么......作为大多数人,你可能会得到一个 RPC API(无论是基于XML还是JSON或其他),它包括一个传输层,用于部分 RESTful 子系统......这是因为拥有 RESTfulnes,意味着灵活性......如果客户端可以或多或少自由地遍历服务器上的数据(通过一组简单的 CRUD 方法),它不依赖于有限的(特定于问题的)一组通过 API 公开的方法,您可以将逻辑转移到客户端...
回答by aehlke
The best way to understand it is to read Roy T. Fielding's dissertation on it, or relevant articles on his blogwhere he discusses the differences between pure REST and simply RPC architectures.
理解它的最好方法是阅读 Roy T. Fielding 关于它的论文,或者他博客上的相关文章,他讨论了纯 REST 和简单的 RPC 架构之间的区别。
Another thing to note is that the Wikipedia article on REST is in dismal condition and Fielding himself, the 'inventor' of REST, suggests that the article is inaccurate.
另一件需要注意的事情是,维基百科上关于 REST 的文章状态不佳,而 REST 的“发明者”菲尔丁本人认为该文章不准确。
The biggest thing people miss with REST is discoverability - resources should include URIs for other related resources inside their hypertext, instead of relying on URI naming conventions, which are out-of-band and non-standardized.
人们在使用 REST 时最大的遗漏是可发现性——资源应该在其超文本中包含其他相关资源的 URI,而不是依赖于带外和非标准化的 URI 命名约定。
A big problem with popular RPC implementations like SOAP or XML-RPC is that they use HTTP underneath their own proprietary architecture, rather than taking advantage of all the different properties of HTTP like PUT, GET, DELETE etc. So this doesn't fit the traditional web stack as well - a cache server in the middle doesn't work, for example, without knowing about the meaning of the contents of the RPC call.
SOAP 或 XML-RPC 等流行 RPC 实现的一个大问题是,它们在自己的专有架构下使用 HTTP,而不是利用 HTTP 的所有不同属性,例如 PUT、GET、DELETE 等。所以这不适合传统的网络堆栈也是如此——例如,在不知道 RPC 调用内容的含义的情况下,中间的缓存服务器不起作用。
This is an incomplete introduction to REST and RPC but I think I've highlighted some of the important points that are often missed. Be careful, since there is a LOT of wrong information out there on REST.
这是对 REST 和 RPC 的不完整介绍,但我想我已经强调了一些经常被遗漏的要点。小心,因为 REST 上有很多错误信息。
That said, REST is not for everything. It's an architecture, so it's rather flexible how you can implement it. But if it doesn't make sense to access things primarily as resources, then REST may not fit, or it may only fit for parts of your application, which is fine.
也就是说,REST 并不是万能的。这是一种架构,因此您可以非常灵活地实现它。但是,如果主要以资源的形式访问事物没有意义,那么 REST 可能不适合,或者它可能只适合您的应用程序的一部分,这很好。
回答by Kalina Todorova
There are three different styles of services:
共有三种不同风格的服务:
- RPC API- the client sends a procedure and parameters to service and the service is responsible for the executing of the command and returning a result.
- Message API (Document API)- the client sends DOMs (elements), which normally are more complex structures than RPC API calls, because they tend to do not imply operations directly.
- Resource API- is used for accessing resources (database tuples, files, images and etc.). In general it should also provide good Media Type Negotiation.
- RPC API- 客户端向服务发送过程和参数,服务负责执行命令并返回结果。
- 消息 API(文档 API)——客户端发送 DOM(元素),它们通常是比 RPC API 调用更复杂的结构,因为它们往往不直接暗示操作。
- 资源 API- 用于访问资源(数据库元组、文件、图像等)。一般来说,它还应该提供良好的媒体类型协商。
SOAP and REST are compilation of standards from W3C, and the main difference is that SOAP uses HTTP, SMTP and etc. as transport protocols and REST uses it as application protocol, AKA it should support (GET, PUT, PUSH, DELETE, and POST). SOAP also implies using XML and REST could use any data type (JSON, XML, HTTP, etc.). Furthermore, one of the main advantages of SOAP is the Service Descriptor (WSDL file), which gives the possibility of auto-generation of Service Connector (proxy) to the client.
SOAP 和 REST 是W3C的标准汇编,主要区别在于 SOAP 使用 HTTP、SMTP 等作为传输协议,REST 使用它作为应用程序协议,AKA 应该支持(GET、PUT、PUSH、DELETE 和 POST )。SOAP 还意味着使用 XML,而 REST 可以使用任何数据类型(JSON、XML、HTTP 等)。此外,SOAP 的主要优点之一是服务描述符(WSDL 文件),它为客户端自动生成服务连接器(代理)提供了可能性。
There is not a silver bullet; the type and architecture of a web service are dependent on the actual client and technology requirements.
没有灵丹妙药;Web 服务的类型和架构取决于实际的客户端和技术要求。
For a general idea on the subject, see one of the Martin Fowler signature books - Service Design Patterns
有关该主题的一般想法,请参阅 Martin Fowler 的其中一本签名书 - Service Design Patterns

