如何在Web应用程序上实现RESTful服务?

发布于 2024-11-16 04:15:44 字数 1183 浏览 0 评论 0原文

我对标题中的问题进行了很多搜索,但我仍然不确定我应该做什么。我今天早上刚听说 RESTful,读得越多,我就越困惑。

几周前,我应该制作一个 PHP/jQuery/AJAX Web 应用程序,它基本上是一个具有多个用户的简单待办事项列表。在此过程中我学到了很多关于 ajax 和 jQuery 的知识(感谢 stackoverflow),我对最终结果非常满意。

该应用程序的工作原理如下: 用户登录,他的 id 存储在会话变量中。例如,如果他单击“添加”按钮,代码将如下所示。

//script.js
$.get("ajax.php",{'action':'new','text': $text, 'list': $selectedList
    },
            function(msg){
                    // add a new task and fade it
                        $(msg).hide().appendTo('.listoftasks').fadeIn();
    });

在 ajax.php 上

//ajax.php

 if($_GET['action'] == '新')
    add_task($_GET['text'], $_GET['list'], $_SESSION['user_id']);

因此,事实证明,用于 Web 应用程序的 Android 应用程序已在计划中,有人问我是否可以为其制作 RESTful Web 服务。任何人都可以为我指出正确的方向吗?是否可以使用这样编码的应用程序来实现这种服务,因为我在某处(以及许多其他内容)读到它不适用于会话?

如果有人至少能向我指出一些基本教程,那就太好了,因为目前这对我来说非常困惑。

感谢您的所有帮助

编辑:

好的,谢谢大家,在您的帮助下我设法完成了我要求的事情,并且我学到了一些新东西,谢谢。我现在遇到的问题是身份验证。基本上,Android 应用程序使用一些变量(id、list_id 等)向我的 api.php 页面发送请求。现在我如何实现某种形式的身份验证,以便 api.php 只返回经过身份验证的用户的结果。我已经与应用程序开发人员进行了简短的交谈,基本上他会通过 http 标头(或类似的东西)向我发送用户名和密码。如何在 api.php 页面上获取这些值? 感谢您的所有帮助:)

i searched a lot regarding the question in the title and I'm still not really sure what I'm supposed to do. I just heard of RESTful this morning and the more I read about it the more confused I get.

A couple of weeks ago I was supposed to make an PHP/jQuery/AJAX web application that would basically be a simple todo list with multiple users. I learned a lot about ajax and jQuery(thanks to stackoverflow) during the process and I am quite happy with the final result.

The application works like this:
The user logs in, and his id is stored in session variable. For example if he clicks on 'Add' button the code looks something like this.

//script.js
$.get("ajax.php",{'action':'new','text': $text, 'list': $selectedList
    },
            function(msg){
                    // add a new task and fade it
                        $(msg).hide().appendTo('.listoftasks').fadeIn();
    });

And on the ajax.php

//ajax.php

 if($_GET['action'] == 'new')
    add_task($_GET['text'], $_GET['list'], $_SESSION['user_id']);

So, as it turns out an android app for the web app is in the plans and I was asked if I could make a RESTful web service for it. Can anybody point me in the right direction, is it possible to implement that kind of service with an application coded like this because I've read somewhere (among many other things) that it doesn't work with sessions?

It would be great if someone coud at least point me to some basic tutorials, because at this moment this is all very confusing for me.

Thank you for all your help

edit:

Ok thanks everyone, i managed to do what was asked from me with you help and I learned something new, thank you. The problem I have now is authentication. Basically the android app sends request to my api.php page with some variables (id, list_id etc). Now how do I implement some form of authentication so the api.php would only return results for the authenticated user. I've talked to the app developer briefly and basically he would send me username and password through http header (or something like that). How do I get those values on my api.php page?
Thank you for all the help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-11-23 04:15:44

简而言之 - 一个非常实用的非学术解释 - RESTful Web 服务是一个 Web 服务器,它响应按照 :resource/:action/[:id] 模式构建的请求。

例如,您的用户是一种资源,您可以执行以下操作:

  • GET /users :用户列表
  • GET /users/5000/edit :编辑特定用户
  • GET /users/5000 :显示特定用户
  • GET /users/new :表单用于创建新用户
  • POST /users/ :创建新用户的 POST 请求。
  • PUT /users/5000 :更新用户的 Post 请求。
  • DELETE /users/5000 :删除用户的销毁请求。

它是一个CRUD接口:创建、读取、更新、销毁。

您的会议也是资源。

  • POST /session/create :为用户创建一个新会话。
  • DELETE /session/destroy :删除会话。

有些资源不需要具有所有 CRUD 操作。

有关描述 RESTful 架构的原始文档:

http://www .ics.uci.edu/~fielding/pubs/dissertation/top.htm

对于这个问题进行更实际的检查:

http://www.amazon.com/Service-Oriented-Design-Rails-Addison-Wesley-专业/dp/0321659368

In few words – an a very practical, non academic explanation – a RESTful web service is a web server that answer requests that are structured following a :resource/:action/[:id] pattern.

For example, your users are a resource and you have these actions:

  • GET /users : list of users
  • GET /users/5000/edit : Edit a specific user
  • GET /users/5000 : Show a specific user
  • GET /users/new : Form for creating a new user
  • POST /users/ : A post request that created a new user.
  • PUT /users/5000 : A Post request that updates an user.
  • DELETE /users/5000 : A destroy request that removes an user.

It is a CRUD interface: Create, Read, Update, Destroy.

Your sessions are resources too.

  • POST /session/create : Create a new session for a user.
  • DELETE /session/destroy : Removes a session.

Some resources do not need to be have all CRUD actions.

For the original document that describes the RESTful architecture:

http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

For a more practical examination of this matter:

http://www.amazon.com/Service-Oriented-Design-Rails-Addison-Wesley-Professional/dp/0321659368

路还长,别太狂 2024-11-23 04:15:44

这里有很多有用的信息 http://code.google.com/p/implementing-rest /

请注意,网络上有关 REST 的错误信息多于有效信息。

  • RESTful 与 URL 的外观几乎没有任何关系。
  • 构建 RESTful 系统就是能够构建可以长期发展的分布式系统。这不是一个短期、快速的解决方案。
  • 对 RESTFul 系统的请求应该是相互独立的。除了保留身份验证令牌之外,在请求之间保持类似会话的状态是一个坏主意。

您在网络上看到的大多数被描述为 REST 的系统只是基于 HTTP 的远程过程调用 API。这是一个有效的架构,但它不是 REST,因此不需要遵守 REST 约束,也不一定能获得 REST 的好处。

There is lots of useful information here http://code.google.com/p/implementing-rest/

Be warned, there is more mis-information about REST on the web than there is valid information.

  • Being RESTful has almost nothing to do with what your URL looks like.
  • Building RESTful systems is about being able to build distributed systems that can evolve over the long term. It is not a short term, quick solution.
  • Requests to a RESTFul system should be independent of each other. Keeping session-like state between requests for anything other than holding on to authentication token is a bad idea.

Most of the systems that you will see on the web described as REST are simply HTTP based Remote Procedure Call, APIs. This is a valid architecture, but it is not REST and therefore does not need to comply to the REST constraints, nor does it necessarily gain the benefits of REST.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文