tamkeen-tms/rest-client

为Tamkeen的API(tamkeentms.com)提供的PHP REST客户端

v2.1 2021-06-09 13:16 UTC

README

这是什么?

这是Tamkeen的REST API的官方PHP客户端。您可以在您的PHP应用程序中使用它来与Tamkeen通信。在开始之前,您需要获取“租户ID”和“秘密认证密钥”;您可以在“管理”>“API”下找到这些信息。

安装

您可以使用以下命令通过composer将此库添加到您的项目中:

composer require tamkeen-tms/rest-client

接下来,您将能够通过Tamkeen命名空间在项目中使用它。

它如何工作

此库仅向Tamkeen的API端点发送请求。因此,您需要了解哪些端点可用以及如何选择正确的端点!端点列表可在Tamkeen网站上找到。

创建客户端

在发送请求之前,您需要初始化一个新的“客户端”实例,并传递“租户”和授权密钥。通过此客户端,您将能够发送请求。每个“请求”必须通过“客户端”进行。您可以实例化多个客户端来向多个应用程序副本发送请求!

$client = new Tamkeen\Client([tenant], [secret key]) // New client instance

发送请求

使用创建的客户端,您现在可以发送新的请求,例如:

$request = new Tamkeen\Request($client, 'GET', 'branches'); // Create the request
$response = $request->send();

您需要调用->send(),这将发送请求到应用程序并返回响应。响应将以JSON格式返回。

示例

以下是一个获取分支列表的示例

try{
    $request = $client->request('get', 'branches'); // Create the request
    $response = $request->send(); // Send it, returns the response

}catch(RequestException $e){
    /**
    * Possible error messages include:
    * tenant_not_identified: The tenant id was not provided, or invalid
    * api_disabled: API is disabled for the tenant. (Contact the support team to enable it).
    * api_key_missing: The authentication key missing.
    * api_key_invalid: The authentication key is invalid.
    * api_error: An error happened at the API's level; server error (500).
    **/
}

在这种情况下,$response将返回

{
    "branches": [{
        "id": 1,
        "name": "Headquarters",
        "code": "HQ"
        "currency": "USD"
    }]
}

对于任何进一步的问题,请随时在此创建新的问题。