webleit/zohobooksapi

Zoho Books API v3 - PHP SDK

5.5.8 2024-09-16 12:45 UTC

README

Latest Version on Packagist Total Downloads

Zoho Books API v3 - PHP SDK

这个库是一个PHP SDK,简化了Zoho Books Api版本3(https://www.zoho.com/books/api/v3/)的使用。它提供了一个接口,简化了与API的交互,无需担心实际的REST请求,并使用非常简单的Model类包装各种响应,然后可以使用任何其他库或框架使用这些类。

安装

composer require webleit/zohobooksapi

使用

要使用此库,只需require composer自动加载文件,然后启动库本身。为了让库正常工作,您需要通过zoho api进行认证。

在线模式

require './vendor/autoload.php';

// setup the generic zoho oath client
$oAuthClient = new \Weble\ZohoClient\OAuthClient('[CLIENT_ID]', '[CLIENT_SECRET]');
$oAuthClient->setRefreshToken('[REFRESH_TOKEN]');
$oAuthClient->setRegion(\Weble\ZohoClient\Enums\Region::us());
$oAuthClient->useCache($yourPSR6CachePool);

// setup the zoho books client
$client = new \Webleit\ZohoBooksApi\Client($oAuthClient);
$client->setOrganizationId('[YOUR_ORGANIZATION_ID]');

// Create the main class
$zohoBooks = new \Webleit\ZohoBooksApi\ZohoBooks($client);

离线模式

当您需要自主更新访问令牌时,推荐使用此模式。用于所有“机器到机器”的通信,并且当您使用API进行,例如,与第三方应用程序(如您的ERP或电子商务网站)同步时,这是最好的方式。有关详细信息,请参阅https://github.com/Weble/ZohoClient#example-usage-offline-mode

require './vendor/autoload.php';

// setup the generic zoho oath client
$oAuthClient = new \Weble\ZohoClient\OAuthClient('[CLIENT_ID]', '[CLIENT_SECRET]');
$oAuthClient->setRefreshToken('[REFRESH_TOKEN]');
$oAuthClient->setRegion(\Weble\ZohoClient\Enums\Region::us());
$oAuthClient->useCache($yourPSR6CachePool);
$oAuthClient->offlineMode();

// Access Token
$accessToken = $oAuthClient->getAccessToken();
$isExpired = $oAuthClient->accessTokenExpired();

// setup the zoho books client
$client = new \Webleit\ZohoBooksApi\Client($oAuthClient);
$client->setOrganizationId('[YOUR_ORGANIZATION_ID]');

// Create the main class
$zohoBooks = new \Webleit\ZohoBooksApi\ZohoBooks($client);

API调用

要调用任何API,只需使用在API文档中报告的相同名称。您可以使用getAvailableModules()方法获取支持的API列表

$zohoBooks->getAvailableModules();

例如,您可以使用以下方式获取发票列表

$invoices = $zohoBooks->invoices->getList();

或联系人列表

$contacts = $zohoBooks->contacts->getList();

列表调用

要从模块获取资源列表,请使用getList()方法

$invoices = $zohoBooks->invoices->getList();

您可以通过传递一些参数来过滤结果(请参阅zoho books api文档中的示例)

$invoices = $zohoBooks->invoices->getList(['status' => 'unpaid']);

为了导航页面,只需在getList调用中使用“page”和“per_page”参数

$invoices = $zohoBooks->invoices->getList(['status' => 'unpaid', 'page' => 3, 'per_page' => 200]);

返回类型

任何“列表”API调用都返回一个Collection对象,这是Laravel Collection包。因此,您可以将结果用作Collection,这允许映射、缩减、序列化等。

$invoices = $zohoBooks->invoices->getList();

$data = $invoices->toArray();
$json = $invoices->toJson();

// After fetch filtering in php
$filtered = $invoices->where('total', '>', 200);

// Grouping
$filtered = $invoices->groupBy('customer_id');

任何“资源”API调用都返回一个针对您正在获取的单个资源定制的Model对象。例如,调用

$invoice = $zohoBooks->invoices->get('idoftheinvoice');
$id = $invoice->getId();
$data = $invoice->toArray();
$total = $invoice->total;

将返回一个\Webleit\ZohoBooksApi\Models\Invoice对象,该对象是Arrayable和Jsonable的,因此可以以多种方式使用。

Zoho Books令牌过期说明

  • 每个访问令牌仅有效一小时,并且仅用于定义范围内的操作。
  • 刷新令牌不会过期。在访问令牌过期时使用它来刷新访问令牌。
  • 您每分钟只能生成最多五个刷新令牌。

贡献

发现错误、发送pull请求或改进文档 - 欢迎任何贡献,并非常感激。

版本控制

使用语义版本规范(SemVer)。

版权和许可证

版权Weble Srl,根据MIT许可证。