你可以在 PHP 中使用 REST 吗?如果是这样怎么办?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1628628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:19:37  来源:igfitidea点击:

Can you use REST in PHP? If so how?

phprestrestful-architecture

提问by Zeck

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?

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

采纳答案by elviejo79

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 协议的相同方法应用于客户端-服务器架构设计,而 PHP 已经非常适合处理 HTTP 协议请求,例如 GET 和 POST。PHP 特别适合使开发 REST 服务变得容易。

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

请记住,REST 是已存在的相同 http 模式的应用程序。

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

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

  1. HTML Form
  2. PHP Process
  3. HTML Output in a table
  1. HTML 表单
  2. PHP进程
  3. 表格中的 HTML 输出

So to make it REST you would need to:

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

  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.
  2. PHP process
  3. Output in either JSONor XML. And that is it!

    Is pretty easy.

  1. 接受来自网络的参数。这很容易,因为您将收到作为 get 或 post 的参数......所以它基本上是相同的。
  2. PHP进程
  3. JSONXML 格式输出。就是这样!

    很容易。

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

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

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

在这种情况下,我建议您以flickr API为例,它对开发人员非常友好,易于猜测并且有很好的文档。

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

有关 API 的更多信息,请查看此演示文稿:如何设计好的 API及其重要性 (Joshua Bloch)

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

最后,RESTful API 还应该在有意义的情况下实现 http 协议的 PUT 和 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.

例如,如果您的 api 中有删除操作,则该服务应该从 http 协议接收删除方法。而不是将动作参数作为发布请求的一部分发送的更常见的事情。

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."

编辑:将“默认情况下 PHP 是休息”替换为“因为 REST 是将 HTTP 协议的相同方法应用于客户端-服务器架构设计,并且 PHP 已经非常适合处理 HTTP 协议请求,例如 GET 和 POST。 PHP 特别适合使开发 REST 服务变得容易。”

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

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

回答by James Black

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/

你可能想看看这篇文章和后续: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/

http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/

回答by Jake Sankey

I developed a class that is the PHP native SoapServer class' REST equivalent.

我开发了一个类,它是 PHP 原生 SoapServer 类的 REST 等价物。

You just include the RestServer.php file and then use it as follows.

您只需包含 RestServer.php 文件,然后按如下方式使用它。

class Hello
{
  public static function sayHello($name)
  {
    return "Hello, " . $name;
  }
}

$rest = new RestServer(Hello);
$rest->handle();

Then you can make calls like this:

然后你可以像这样拨打电话:

http://myserver.com/path/to/api?method=sayHello&name=World

(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.)

(请注意,在查询字符串中提供参数的顺序无关紧要。此外,参数键名称和方法名称不区分大小写。)

回答by Sixten Otto

Can't hurt to go back to the original source of the term REST, and be sure that you understand what that means.

回到术语 REST 的原始来源并确保您理解这意味着什么。

回答by Bj?rn Otto Vasbotten

If you are thinking about the client side of things, I would suggest checking out Matt Sukowski's PEST.

如果您正在考虑事情的客户端,我建议您查看 Matt Sukowski 的 PEST。

You will find the repository on GitHub: https://github.com/educoder/pest

您将在 GitHub 上找到存储库:https: //github.com/educoder/pest

Also check out this thread: PHP REST Clients

另请查看此线程: PHP REST 客户端

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 :)

2013 年 12 月 13 日更新:
这是一个非常活跃的开源项目,Matt Sukowsky 今年夏天将它交给了新的看管人,因为他觉得他没有足够的时间,从那时起就有很多很多的提交。所以 Pest 在 PHP 中做 Rest 比以往任何时候都好:)