使用 PHP 创建 REST API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21959312/
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 04:34:33  来源:igfitidea点击:

Creating a REST API using PHP

phpjsonapirestcurl

提问by Piya

I'm creating my first API to which if two values are passed, I should get the response in the JSON format. The number will be passed as parameters by POST. Either using cURL or whatever POST method is available.

我正在创建我的第一个 API,如果传递两个值,我应该得到 JSON 格式的响应。该数字将通过 POST 作为参数传递。使用 cURL 或任何可用的 POST 方法。

Even though this is a very basic one, I would like to know the best practices and the API should be created by model–controller basis. Not just plain PHP.

尽管这是一个非常基本的方法,但我想知道最佳实践,并且 API 应该以模型-控制器为基础创建。不仅仅是普通的 PHP。

I have Googled for many REST API tutorials. They were good and I have acquired some knowledge on it.

我在 Google 上搜索了许多 REST API 教程。他们很好,我已经获得了一些知识。

But I would like to get a sample model of the code so that I can refer to it and build my own, and that sample of course in the standard practice of making a real REST API.

但我想获得代码的示例模型,以便我可以参考它并构建我自己的模型,当然,该示例是在制作真正的 REST API 的标准实践中。

If you ask me what I have tried, it would be really fun, as a beginner, all I could do is this:

如果你问我尝试过什么,那真的很有趣,作为初学者,我所能做的就是:

$num1 = $_REQUEST['num1'];
$num2 = $_REQUEST['num2'];

$total = $num1 + $num2;
echo json_encode($total);

Of course this can never be called an API, but still. If I give a POST response to this, I want the response from the REST API as JSON. I should be able to test it by REST console as well so that I get a standard response.

当然,这永远不能称为 API,但仍然如此。如果我对此给出 POST 响应,我希望来自 REST API 的响应为 JSON。我也应该能够通过 REST 控制台对其进行测试,以便获得标准响应。

Please do provide me with a very basic but yet a standard RESTful API.

请为我提供一个非常基本但又标准的 RESTful API。

回答by Martin Bean

In your example, it's fine as it is: it's simple and works. The only things I'd suggest are:

在您的示例中,它很好:它简单且有效。我唯一建议的是:

  1. validating the data POSTed
  2. make sure your API is sending the Content-Typeheader to tell the client to expect a JSON response:

    header('Content-Type: application/json');
    echo json_encode($response);
    
  1. 验证发布的数据
  2. 确保您的 API 正在发送Content-Type标头以告诉客户端期待 JSON 响应:

    header('Content-Type: application/json');
    echo json_encode($response);
    

Other than that, an API is something that takes an input and provides an output. It's possible to “over-engineer” things, in that you make things more complicated that need be.

除此之外,API 是接受输入并提供输出的东西。有可能“过度设计”的东西,因为你让事情变得更加复杂。

If you wanted to go down the route of controllers and models, then read up on the MVC patternand work out how your domain objects fit into it. Looking at the above example, I can see maybe a MathControllerwith an add()action/method.

如果你想沿着控制器和模型的路线走下去,那么阅读MVC 模式并找出你的域对象如何适应它。看上面的例子,我可以看到一个MathController带有add()动作/方法的。

There are a few starting point projects for RESTful APIs on GitHubthat are worth a look.

GitHub 上有几个 RESTful API 的起点项目值得一看。

回答by Nikolaos Dimopoulos

Trying to write a REST API from scratch is not a simple task. There are many issues to factor and you will need to write a lot of code to process requests and data coming from the caller, authentication, retrieval of data and sending back responses.

尝试从头开始编写 REST API 并不是一项简单的任务。有很多问题需要考虑,您将需要编写大量代码来处理来自调用者的请求和数据、身份验证、数据检索和发回响应。

Your best bet is to use a framework that already has this functionality ready and tested for you.

您最好的选择是使用一个已经为您准备好并测试过此功能的框架。

Some suggestions are:

一些建议是:

Phalcon - REST API building- Easy to use all in one framework with huge performance

Phalcon - REST API 构建- 易于在一个框架中使用并具有巨大的性能

Apigility- A one size fits all API handling framework by Zend Technologies

Apigility- Zend Technologies 的一种适合所有 API 处理框架

Laravel API Building Tutorial

Laravel API 构建教程

and many more. Simple searches on Bitbucket/Github will give you a lot of resources to start with.

还有很多。Bitbucket/Github 上的简单搜索将为您提供大量资源。