使用PHP创建一个REST API(Create a REST API with PHP) 节选1

6 篇文章 0 订阅
5 篇文章 0 订阅

        最近项目需要用到REST,故收集相关资料。

        One of the latest (sort of) crazes sweeping the net is APIs, more specifically those that leverage REST. It’s really no surprise either, as consuming REST APIs is so incredibly easy… in any language. It’s also incredibly easy to create them as you essentially use nothing more than an HTTP spec that has existed for ages. One of the few things that I give Rails credit for is its well thought-out REST support, both for providing and consuming these APIs (as its been explained by all the Rails fanboys I work with). 

         最近互联网上比较热门的一个名词是APIs(接口),特别是leverage REST。不过考虑到REST APIs在任何语言下都是非常的简单,也就没什么好惊奇的了。同时,它也是非常的容易创建,你基本只需要使用已经存在多年的HTTP规范就可以。我认为Rails语言的为数不多的优点之一就是良好的REST支持,不仅是提供APIs,同时也有很多的客户端支持(我的一些Rails粉丝同事都向我解释了这一点)。


       Seriously, if you’ve never used REST, but you’ve ever had to work with (or worse, create) a SOAP API, or simply opened a WSDL and had your head explode, boy do I have good news for you! 
       认真的讲,假如你从来没有使用过REST,却曾经使用过SOAP API,或者只是简单的打开一个令人头大的WSDL文档。小伙子,我确实要带给你一个好消息!


      So, What on Earth is REST? Why Should You Care? 
      那么,究竟什么是REST?为什么你应该关心?


      Before we get into writing some code, I want to make sure everyone’s got a good understanding of what REST is and how its great for APIs. First, technically speaking, REST isn’t specific to just APIs, it’s more of a generic concept. However, obviously, for the sake of this article we’ll be talking about it in the context of an API. So, let’s look at the basic needs of an API and how REST addresses them. 
      在我们开始写代码之前,我想要确认每个人都可以很好的理解什么是REST以及它是如何特别适合APIs的。首先,从技术上来讲,REST并不是仅仅特定于APIs应用,它更多的是一个通用的概念。然而,很明显,我们这篇文章所讨论的REST就是在接口应用的环境下。因此,让我们看看一个API的基本要求已经REST如何处理他们。 


Requests 请求

All APIs need to accept requests. Typically, with a RESTful API, you’ll have a well-defined URL scheme. Let’s say you want to provide an API for users on your site (I know, I always use the “users” concept for my examples). Well, your URL structure would probably be something like, “api/users” and “api/users/[id]” depending on the type of operation being requested against your API. You also need to consider how you want to accept data. These days a lot of people are using JSON or XML, and I personally prefer JSON because it works well with JavaScript, and PHP has easy functionality for encoding and decoding it. If you wanted your API to be really robust, you could accept both by sniffing out the content-type of the request (i.e. application/json or application/xml), but it’s perfectly acceptable to restrict things to one content type. Heck, you could even use simple key/value pairs if you wanted. 

所有的APIs都需要接收请求。对于一个RESTful API,你需要一个定义好的URL规则,我们假定你想要提供一个接口给你网站上的用户(我知道,我总是使用"用户"这个概念来举例)。你的URL结构可能类似于这样:"api/users"或者"api/users/[id]",这取决于请求接口的操作类型。同时,你也需要考虑你想要怎么样接收数据。近来一段时间,很多人正在使用JSON或者XML,从我个人来讲,我更加喜欢JSON,因为它可以很好的和javascript进行交互操作,同时PHP也可以很简单的通过json_encode和json_decode两个函数来对它进行编码和解码。如果你希望自己的接口真正强健,你应该通过识别请求的内容类型(比如application/json或者application/xml)同时允许接收两种格式。但是,限制只接收一种类型的数据也是可以很好的被接受。真见鬼,假如你愿意,你甚至可以使用简单的键/值对。


The other piece of a request is what it’s actually meant to do, such as load, save, etc. Normally, you’d have to come up with some sort of architecture that defines what action the requester (consumer) desires, but REST simplifies that. By using HTTP request methods, or verbs, we don’t need to define anything. We can just use the GET, POST, PUT, and DELETE methods, and that covers every request we’d need. You can equate the verbs to your standard crud-style stuff: GET = load/retrieve, POST = create, PUT = update, DELETE = well, delete. It’s important to note that these verbs don’t directly translate to CRUD, but it is a good way to think about them. So, going back to the above URL examples, let’s take a look at what some possible requests could mean: 
    GET request to /api/users – List all users 
    GET request to /api/users/1 – List info for user with ID of 1 
    POST request to /api/users – Create a new user 
    PUT request to /api/users/1 – Update user with ID of 1 
    DELETE request to /api/users/1 – Delete user with ID of 1 
一个请求的其他部分是它真正要做的事情,比如加载、保存等。通常来说,你应该提供几种结构来定义请求者(消费者)所希望的操作,但是REST简化了这些。通过使用HTTP请求方法或者动作,我们不需要去额外定义任何东西。我们可以仅仅使用GET,POST,PUT和DELETE方法,这些方法涵盖了我们所需要的每一个请求。你可以把它和标准的增删改查模式对应起来:GET=加载/检索(查,select),POST=创建(增,Create),PUT=更新(改,update),DELETE=删除(DELETE)。我们要注意到,这些动词并没有直接翻译成CRUD(增删改查),但是这个理解它们的一个很好的方法。因此,回到刚才所举的URL的例子,让我们看一下一些可能的请求的含义: 
    GET request to /api/users – 列举出所有的用户 
    GET request to /api/users/1 – 列出ID为1的用户信息 
    POST request to /api/users – 插入一个新的用户 
    PUT request to /api/users/1 – 更新ID为1的用户信息 
    DELETE request to /api/users/1 – 删除ID为1的用户



As you hopefully see, REST has already taken care of a lot of the major headaches of creating your own API through some simple, well-understood standards and protocols, but there’s one other piece to a good API… 

正如你所希望看到的,REST已经解决了很多令人头疼的创建接口的问题,通过一些简单的,容易理解的标准和协议。但是一个好的接口还要另外一个方面...


目录:

使用PHP创建一个REST API(Createa REST API with PHP) 节选1

使用PHP创建一个REST API(Createa REST API with PHP) 节选2

使用PHP创建一个REST API(Createa REST API with PHP) 节选3

使用PHP创建一个REST API(Create aREST API with PHP) 节选4

使用PHP创建一个REST API(Createa REST API with PHP) 节选5

使用PHP创建一个REST API(Createa REST API with PHP) 节选6


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值