asimlqt/netsuite-rest-api-php

Netsuite 的 REST API 客户端

0.5.0 2024-06-03 08:31 UTC

This package is auto-updated.

Last update: 2024-09-03 08:57:58 UTC


README

要求

  • PHP >= 8.1
  • PSR-7, PSR-17 和 PSR-18 实现

安装

composer require asimlqt/netsuite-rest-api-php

用法

第一步是创建一个实例工厂,传入 netsuite API 设置。所有参数都是必需的。

$factory = new NetsuiteRestApi\NetsuiteClientFactory(
    $companyUrl,
    $accountId,
    $consumerKey,
    $consumerSecret,
    $tokenId,
    $tokenSecret
);

之后调用 create 方法,这将返回一个 REST API 客户端实例。

$client = $factory->create();

现在您可以在客户端上调用任何端点,例如:

try {
    $response = $client->customer->get('1234');
} catch (ApiException $e) {
    echo $e->getMessage();
}

错误处理

请确保将所有 API 调用包裹在 try/catch 中。当 API 调用失败时,将抛出一个异常 NetsuiteRestApi\Client\ApiException

要获取实际的响应对象,只需调用异常实例的 getResponse() 方法。这将返回一个 Psr\Http\Message\ResponseInterface

可用的 API 方法

每个端点都有以下 6 个方法

  • list
  • get
  • insert
  • delete
  • update
  • upsert

注意:并非所有这些方法都在 Netsuite 的每个端点中都可用。目前,此客户端不会阻止您调用不可用的方法,但您将从 Netsuite 收到错误。

分页

唯一支持分页的端点是 list。这将返回一个实现 Iterator 的游标,您可以像通常一样迭代它。

try {
    $cursor = $client->currency->list();
    foreach ($cursor as $item) {
        echo $item['id'];
    }
} catch (ApiException $e) {
    echo $e->getMessage();
}

游标将迭代单个实体。当迭代完所有实体后,如果可用,它将自动获取下一页。

注意:即使您提供了限制查询参数,游标仍然会迭代 Netsuite 中所有可用的实体。

$cursor = $client->currency->list(['limit' => 3]);

限制查询参数仅提供在每次 API 调用中设置要获取的实体数量的功能。

如果您只想迭代一组实体,即使有更多可用,也可以将游标包裹在 LimitIterator 中。

 $cursor = $client->customer->list(['limit' => 10]);
 foreach (new LimitIterator($cursor, 0, 10) as $item) {
    ...
 }