简体   繁体   English

设置RESTful Web服务

[英]Setting up a RESTful web service

I'm just getting into using REST and have started building my first app following this design model. 我刚刚开始使用REST,并已开始按照此设计模型构建我的第一个应用程序。 From what I can gather the idea is to build your service like an api which your website itself is a consumer of. 据我所知,我们的想法是像您自己的网站的使用者一样使用api来构建您的服务。

This makes sense for me since my web app does a lot of AJAX calls, however it seems a little wasteful to authenticate each request to avoid using sessions. 这对我来说很有意义,因为我的Web应用程序执行了许多AJAX调用,但是对每个请求进行身份验证以避免使用会话似乎有点浪费。 Is this just something I have to accept as part of the REST design process? 这只是我在REST设计过程中必须接受的东西吗?

Also, making ajax calls works fine, but say, I need to just show a view of the users profile, does this now mean I also need to make a curl call to my api to pull this data. 同样,进行ajax调用也可以,但是,我只需要显示用户个人资料的视图,这是否意味着我现在还需要对我的api进行curl调用以提取此数据。 At this point I know I'm working internally so is authentication even required? 在这一点上,我知道我在内部工作,所以甚至需要身份验证吗?

Some remarks: 一些说明:

While you could set up your whole application to have a REST interface , you should set it up to still be able to call it internally. 虽然您可以将整个应用程序设置为具有REST 接口 ,但应将其设置为仍然能够在内部调用它。 Calling it from HTTP, and getting results back by HTTP is only input-processing, and output-rendering. 从HTTP调用它,然后通过HTTP获取结果只是输入处理和输出呈现。 So, if you seperate those concerns you get a flow: input-processing -> method call -> data return -> data rendering . 因此,如果您将这些关注点分开,则会得到一个流程: input-processing -> method call -> data return -> data rendering Shaving of the first & last bit, what do you have left? 刮胡子的第一和最后一点,您还剩下什么? A function call that returns data, which you can just use in your code. 返回数据的函数调用,您可以在代码中使用它们。 Seperate functionality to translate an 'outside' function call into an 'internal' one, and render 'internal' data into 'external' (xml, json, html, whatever you desire), makes your app efficient, and still fully REST capable. 单独的功能可将“外部”函数调用转换为“内部”函数调用,并将“内部”数据呈现为“外部”(xml,json,html,无论您需要什么),使您的应用程序高效且仍具有完全的REST功能。

Authentication is needed if you allow outside calls, even if you don't 'tell' other users data can be retrieved a certain way, it is still easily discoverable. 如果您允许外线呼叫,则需要进行身份验证,即使您不“告诉”其他用户,也可以通过某种方式检索数据,但仍很容易发现它。 I don't know why you wouldn't want to use sessions for this authentication (which most likely happens in forementioned translation from an 'outside' call to an internal one. I would not make 'not using sessions' a requirement, but there is no reason you couldn't allow several methods of authentication (session, re-authentication on every request, tokens, etc.). 我不知道您为什么不希望使用会话进行此身份验证(这很可能发生在从“外部”调用到内部调用的上述转换中。我不会要求“不使用会话”,但是没有理由您不允许几种身份验证方法(会话,每个请求的重新身份验证,令牌等)。

Typically I prefer to produce an interface which can be called using standard PHP and then add an interface to this which adds authentication and RESTful access. 通常,我更喜欢产生一个可以使用标准PHP调用的接口,然后向其添加一个接口,以添加身份验证和RESTful访问。 So you can access for example: 因此,您可以访问例如:

http://example/api/fetchAllFromUsers?auth-key=XXXXX

Which translates to: 转换为:

$internalInterface = new Api();
$internalInterface->fetchAllFromUsers();

Instead of authenticating each time, save a chunk of state (in, eg, a cookie) that identifies your session, and use that. 不用每次都进行身份验证,而是保存一个状态块(例如在cookie中),然后使用该状态块。 It then becomes either a parameter to a GET (using the ?name-value syntax) or can be a part of the URI itself, eg 然后,它要么成为GET的参数(使用?name-value语法),要么可以成为URI本身的一部分,例如

 http://example.com/application/account/ACCTNO/TOKEN

where ACCTNO and TOKEN identify the account and the authentic session respectively. 其中ACCTNOTOKEN标识帐户和真实会话。

This may seem a little flaky at first, but it means that your application, as it grows larger, never needs complicated load-balancing with session state and so on -- a simple proxy scheme works fine. 乍一看似乎有点不稳定,但这意味着您的应用程序随着它的变大而不再需要使用会话状态进行复杂的负载平衡等等,一个简单的代理方案就可以很好地工作。 This reduces the architeccture complexity by great staggering amounts. 这大大减少了架构复杂性。

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

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