可以在PHP中使用REST吗?如果可以,怎么办? [英] Can you use REST in PHP? If so how?

查看:137
本文介绍了可以在PHP中使用REST吗?如果可以,怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自己的PHP库,我想从我的API调用RESTful Web服务.可以在PHP中完成吗?如果可以,这样做的基础是什么?

I am developing my own PHP Library and I would like to call RESTful web-services from my API. Can this be done in PHP and if so what are the basics in doing so?

推荐答案

由于REST是HTTP协议的相同方法在客户端-服务器体系结构设计中的应用,因此PHP已经很好地处理了HTTP协议请求,例如作为GET和POST. PHP特别适合使REST服务的开发变得容易.

Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy.

记住REST是已经存在的相同http模式的应用程序.

Remember REST is the application of the same http patterns that already exists.

因此,如果您当前有一个执行以下操作的应用程序:

So if you currently have an application that does something like:

  1. HTML表单
  2. PHP进程
  3. 表格中的HTML输出

因此要使其成为REST,您需要:

So to make it REST you would need to:

  1. 接受来自网络的参数.这很容易,因为您将以get或post形式接收参数...因此基本上是相同的.

  1. Accept parameters from the web. This is easy since you will receive the parameters either as get or post... so it is basically the same.

PHP进程

JSON XML 输出.就是这样!

非常简单.

现在困难的部分是使您将生成的API(函数和URL)变得对程序员友好.

Now the difficult part is to make your API (the functions and URLs) that you will generate to be programmer friendly.

在这种情况下,我建议您查看 flickr API ,因为它非常适合开发人员友好,易于猜测,并具有良好的文档说明.

In that case I suggest you look at the flickr API as an example is very developer friendly easy to guess and has good documentation.

有关API的更多信息,请参见以下演示文稿:如何设计良好的API&为何如此重要(Joshua Bloch)

For more info on APIs look at this presentation: How to Design a Good API & Why it Matters (Joshua Bloch)

最后,在有意义的情况下,RESTful API还应实现http协议的PUT和DELETE方法

Finally a RESTful API should implement also the PUT and DELETE methods of the http protocol when it makes sense

例如,如果您在api中执行了删除操作,则该服务应从http协议接收delete方法.而不是将动作参数作为发布请求的一部分发送出去的常见做法.

For example if you had a delete action in your api, said service should receive the delete method from the http protocol. Instead of the more common thing of sending an action parameter as part of a post request.

编辑:替换为默认情况下,Php处于静止状态" 因为REST是HTTP协议的相同方法在客户端-服务器体系结构设计中的应用,并且PHP已经很好地处理了诸如GET和POST之类的HTTP协议请求. PHP特别适合使REST服务的开发变得容易."

Replaced "Php is rest by default" with "Since REST is the application of the same methods of the HTTP protocol to the design of client-server architectures and PHP is already so good to handle HTTP protocol requests such as GET and POST. PHP is specially suited to make developing REST services easy."

并且还添加了最后的说明,即当该操作对您的api有意义时,您应该实现适当的PUT或DELETE方法.

And also added the final note that you should implement the appropiate PUT or DELETE methods when that action makes sense for your api.

这篇关于可以在PHP中使用REST吗?如果可以,怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆