可以在 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于 REST 是将 HTTP 协议的相同方法应用到客户端-服务器架构的设计中,并且 PHP 已经可以很好地处理 GET 和 POST 等 HTTP 协议请求。 PHP 特别适合简化 REST 服务的开发。
请记住,REST 是已存在的相同 http 模式的应用程序。
因此,如果您当前有一个应用程序执行以下操作:
因此,要使其成为 REST,您需要:
接受来自 Web 的参数。这很容易,因为您将收到 get 或 post 形式的参数...所以它基本上是相同的。
PHP进程
以 JSON 或 XML 形式输出。就是这样!
非常简单。
现在困难的部分是使您将生成的 API(函数和 URL)对程序员友好。
在这种情况下,我建议您查看 flickr API 作为示例,它对开发人员非常友好,易于使用猜测并有很好的文档。
有关 API 的更多信息,请参阅此演示文稿:如何设计良好的 API 和 API为什么它很重要(Joshua Bloch)
最后,RESTful API 还应该实现 http 协议的 PUT 和 DELETE 方法在有意义的情况下
例如,如果您的 api 中有一个删除操作,所述服务应该从http协议接收删除方法。而不是更常见的将操作参数作为发布请求的一部分发送。
编辑:将“Php 默认是 REST”替换为“因为 REST 是将 HTTP 协议的相同方法应用于客户端-服务器架构的设计,并且 PHP 已经可以很好地处理 HTTP 协议” PHP 等请求特别适合简化 REST 服务的开发。”
还添加了最后的注释,即当该操作对您的 api 有意义时,您应该实现适当的 PUT 或 DELETE 方法。
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.
Remember REST is the application of the same http patterns that already exists.
So if you currently have an application that does something like:
So to make it REST you would need to:
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 process
Output in either JSON or XML. And that is it!
Is pretty easy.
Now the difficult part is to make your API (the functions and URLs) that you will generate to be programmer friendly.
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.
For more info on APIs look at this presentation: How to Design a Good API & Why it Matters (Joshua Bloch)
Finally a RESTful API should implement also the PUT and DELETE methods of the http protocol when it makes sense
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.
Edit: 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."
And also added the final note that you should implement the appropiate PUT or DELETE methods when that action makes sense for your api.
您可能想看看这篇文章和后续文章:
http://www.gen-x -design.com/archives/create-a-rest-api-with-php/
您的问题非常开放,因此本教程可能是最好的起点。
上面的链接不再有效,因此请查看本教程:
http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/
You may want to look at this article and the follow-up:
http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
Your question is very open-ended, so this tutorial may be the best starting point.
The link above is no longer working so check out this tutorial:
http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/
我开发了一个与 PHP 本机 SoapServer 类的 REST 等效的类。
您只需包含 RestServer.php 文件,然后按如下方式使用它。
然后您可以进行如下调用:(
请注意,查询字符串中提供的参数顺序并不重要。此外,参数键名称和方法名称不区分大小写。)
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
Then you can make calls like this:
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
返回REST 一词的原始来源也没什么坏处,并确保您理解这意味着什么。
Can't hurt to go back to the original source of the term REST, and be sure that you understand what that means.
如果您正在考虑客户端方面的问题,我建议您查看 Matt Sukowski 的 PEST。
您将在 GitHub 上找到该存储库:
https://github.com/educoder/pest
另请查看此线程:
PHP REST 客户端
2013 年 12 月 13 日更新:
这在很大程度上是一个实时的开源项目,Matt Sukowsky 今年夏天将其移交给了新的管理员,因为他觉得自己没有足够的时间,从那时起已经有很多很多的提交。所以 Pest 比以往任何时候都更好地使用 PHP 进行 Rest :)
If you are thinking about the client side of things, I would suggest checking out Matt Sukowski's PEST.
You will find the repository on GitHub:
https://github.com/educoder/pest
Also check out this thread:
PHP REST Clients
Update 2013/12/13:
This is very much a live open source project, Matt Sukowsky handed it over to new caretakers this summer because he didnt feel he could spare enough time, and there has been lots and lots of commits since then. So Pest is better than ever for doing Rest in PHP :)