如何在 PHP 中创建 Web API 服务

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

How to create web API service in PHP

phpweb-serviceswsdl

提问by ashokostech

Possible Duplicate:
Is it necessary to have a wsdl file for the creation of a webservice in php?

可能的重复:
是否有必要有一个 wsdl 文件来在 php 中创建 web 服务?

I want to create a web API.

我想创建一个 Web API。

My requirement is that I want to create to a way so that my customers would be able to insert/delete data from my website database, but I do not want the customers directly logged into my site. I want them to call my WSDL file URL via a client request, which will return the response to the customer.

我的要求是我想创建一种方式,以便我的客户能够从我的网站数据库中插入/删除数据,但我不希望客户直接登录到我的网站。我希望他们通过客户端请求调用我的 WSDL 文件 URL,这会将响应返回给客户。

I have heard that we can do so with the help of SOAP call or NuSOAP. I have tried several methods after doing some googling but have no success.

我听说我们可以在 SOAP 调用或 NuSOAP 的帮助下做到这一点。我在谷歌搜索后尝试了几种方法,但没有成功。

Can you please tell me the step by step instructions how I can achieve this.

你能告诉我如何实现这一点的分步说明吗?

回答by codersofthedark

Basically, What is an API?

基本上,什么是 API?

If you understand how data goes from a HTML form to php code, then trust me you know all about API's. Here rather than talking forms, we talk about urls that are created at the end by those forms.

如果您了解数据如何从 HTML 表单转换为 php 代码,那么相信我,您对 API 了如指掌。在这里,我们不讨论表单,而是讨论由这些表单在最后创建的 url。

What are the Types of API?

API有哪些类型?

There are two types of heavily used APIs for web services: SOAP and REST. Google is one of the major players with a SOAP based API while Yahoo (and most of their recent acquisitions) have taken the REST approach. More often than not, a “Web 2.0” service you come across today will probably be using REST.

有两种广泛使用的 Web 服务 API:SOAP 和 REST。谷歌是基于 SOAP 的 API 的主要参与者之一,而雅虎(以及他们最近的大部分收购)采用了 REST 方法。通常情况下,您今天遇到的“Web 2.0”服务可能会使用 REST。

How to create a REST API in PHP with Authentication Key to delete a value from database?

如何使用身份验证密钥在 PHP 中创建 REST API 以从数据库中删除值?

Let's say we have a PHP class (manage.php) that helps us manage entries in a database:

假设我们有一个 PHP 类 (manage.php) 可以帮助我们管理数据库中的条目:

class manage {
    private $entryId;

    function __construct($entryId) {
        $this->entryId = $entryId;
    }

    function deleteEntry() {
        //delete $this->entryId from database
    }
}

On our own server, we might access this functionality like so:

在我们自己的服务器上,我们可能会像这样访问这个功能:

require_once('manage.php');
$m = new manage(23);
$m->deleteEntry();

Easy enough for us, but how do we allow someone not on our server access to the same functionality? For that, we'll create a third file to act as a buffer (or “interface”) between other developers and our class. Here's an example of a file we might create to allow developers to access the delete function in our class, we'll locate it at ‘api/delete.php'

对我们来说很简单,但是我们如何允许不在我们服务器上的人访问相同的功能?为此,我们将创建第三个文件作为其他开发人员和我们班级之间的缓冲区(或“接口”)。这是我们可能创建的一个文件示例,以允许开发人员访问我们类中的删除功能,我们将在“api/delete.php”中找到它

require_once('manage.php');
if (hasPermission($_POST['api_key']) {
    $m = new manage($_POST['entry_id']);
    $m->deleteEntry();
}

This will allow users to send a POST request to us at http://example.com/api/delete.phpwith an api_key and an entry_id. You'll notice the function is very similar to what we wrote on our own server except we check the POST api_key variable using a function to see if its authorized to access the database. We didn't include that function here (hasPermission) for simplicity's sake. In addition to what's shown, you'd also have to find the user's account based off of the api_key, put in some error checking and (if you want to make a good API) provide a properly formatted success or error response after the request. We'll get into the success and error responses in a bit.

这将允许用户通过 api_key 和 entry_id在http://example.com/api/delete.php向我们发送 POST 请求。您会注意到该函数与我们在自己的服务器上编写的函数非常相似,只是我们使用函数检查 POST api_key 变量以查看其是否有权访问数据库。为简单起见,我们没有在此处包含该函数 (hasPermission)。除了显示的内容之外,您还必须根据 api_key 找到用户的帐户,进行一些错误检查,并且(如果您想制作一个好的 API)在请求之后提供格式正确的成功或错误响应。稍后我们将讨论成功和错误响应。

How To Design A Good API and Why it Matters?

如何设计一个好的 API 以及它为什么重要?

I guess this video by Google can explain lot better than me. http://youtu.be/aAb7hSCtvGw

我想谷歌的这个视频比我解释得更好。http://youtu.be/aAb7hSCtvGw

References:

参考:

http://en.wikipedia.org/wiki/Representational_state_transfer

http://en.wikipedia.org/wiki/Representational_state_transfer

http://en.wikipedia.org/wiki/SOAP

http://en.wikipedia.org/wiki/SOAP

http://particletree.com/features/how-to-add-an-api-to-your-web-service/

http://particletree.com/features/how-to-add-an-api-to-your-web-service/

http://www.webresourcesdepot.com/how-to-create-an-api-10-tutorials/

http://www.webresourcesdepot.com/how-to-create-an-api-10-tutorials/

Google Search Result

谷歌搜索结果

Note:The answer is compilation of all references!!.

注意:答案是所有参考资料的汇编!!。