art-pub/nfon-api-client-php

NFON 管理门户 REST API PHP 客户端

1.0.0 2023-09-27 16:21 UTC

This package is auto-updated.

Last update: 2024-09-27 18:27:09 UTC


README

这是一个用于向 NFON 管理门户 REST API 发送请求的小型 PHP 包。

要求

您需要以下内容:

  1. API 的主要 URL(例如,https://api09.nfon.com
  2. 您的 API 公钥(例如,NFON-ABCDEF-123456
  3. 您的 API 密钥(例如,gAp3bTxxUev5JkxOcBdeC5Absm7J84jp6mEhJZd3XiLdjzoGSF
  4. 您的账户名称(例如,K1234

注意

如果您没有这些信息,请联系您的 NFON 合作伙伴以获得帮助。

警告

切勿分享您的 API 密钥

使用方法

安装包

安装的最简单方法是使用 composer 使用此包

bash$ composer.phar require art-pub/nfon-api-client-php

创建请求

    ...
    use NFONAPIClient\client;
    ...
    $apiBasePath = "https://api09.nfon.com"; // <-- insert the API base path provided by NFON
    $apiKey      = "NFON-KEY"; // <-- insert the API key provided by NFON
    $apiSecret   = "NFON-SECRET"; // <-- insert the API secret here
    $account     = "NFON-ACCOUNT"; // <-- insert the account name here
    $path = "/api/customers/" . $account . "/phone-books";
    $method = "GET";
    $body = "";
    $contentType = "application/json";

    // calculate the signature
    [$requestDate, $requestContentType, $requestContentMD5, $signature] = client::getAuthentication($path, $apiSecret, $method, $body, $contentType);

    // make the request
    [$response, $success] = client::request(
            apiBasePath: $apiBasePath,
            apiKey: $apiKey,
            signature: $signature,
            method: $method,
            path: $path,
            apiDate: $requestDate,
            contentMD5: $requestContentMD5,
            body: $body,
            contentLength: strlen($body),
            contentType: $requestContentType,
            apiHeaders: [], // <-- you can add additional request headers here
        );
    if ($success) {
        print_r($response);
        print PHP_EOL;
    } else {
        print "ERROR: $response".PHP_EOL;
    }
    ...

结果是类似 JSON 的格式,但难以阅读或处理。因此,$response 将具有一个键 dataMap,它包含以更有用的结构的结果,原始数据仍保留在 $response['data'](对于单个结果)或 $response['items'](对于多个结果)中。

原始数据

...
[items] => Array
        (
            [0] => Array
                (
                    [href] => /api/customers/K1234/phone-books/1234
                    [links] => Array
                        (
                        )

                    [data] => Array
                        (
                            [0] => Array
                                (
                                    [name] => phonebookEntryId
                                    [value] => 1234
                                )

                            [1] => Array
                                (
                                    [name] => displayName
                                    [value] => Some User
                                )

                            [2] => Array
                                (
                                    [name] => displayNumber
                                    [value] => +49 (4711) 8015
                                )

                            [3] => Array
                                (
                                    [name] => restricted
                                    [value] => 
                                )

                        )

                )
...

数据映射

...
    [dataMap] => Array
        (
            [0] => Array
                (
                    [phonebookEntryId] => 1234
                    [displayName] => Some User
                    [displayNumber] => +49 (4711) 0815
                    [restricted] => 
                )
...

须知

数据集和分页

返回多个记录的端点将在第一次请求中返回最多 100 条记录。结果包含以下信息

Href:当前请求的路径

Total:所有数据集的数量(不是页面!)

Offset:从 0 开始的偏移量

Size:响应中最大结果的数量。您可以通过请求中的参数 pageSize=XXX 设置数量,其中 XXX 为最大 100。

Links:包含第一个、下一个和最后一个 URL 的链接数组,以检索所有数据。请参阅上面示例中的 LinksMap["first"]LinksMap["last"]LinksMap["next"]

重要

请注意:您必须遍历所有这些链接来检索所有数据。只需重复使用 next 提供的 Href,直到当前 Href(当前请求的路径)与 last 条目匹配。

重要

如果 last 条目为空,则当前响应中已包含所有数据。

链接