rams007/zohobooksapi

Zoho Books API v3 - PHP SDK

5.5.9 2024-05-01 11:00 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

用法

为了使用此库,只需引入composer自动加载文件,然后启动库本身。为了使库能够工作,您需要通过zoho apis进行身份验证。

在线模式

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令牌过期说明

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

贡献

查找错误、发送拉取请求或改进文档——任何贡献都受欢迎并高度赞赏。

版本控制

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

版权和许可证

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