mradcliffe / xeroclient

提供用于与Xero会计和工资API一起使用的Guzzle客户端。

0.5.3 2024-06-19 15:56 UTC

This package is auto-updated.

Last update: 2024-09-19 16:35:21 UTC


README

xeroclient是一个PHP库,它扩展了Guzzle以提供与Xero API的集成。它主要用于作为您自己项目的API层。它支持连接到会计API、工资API和文件API URL,作为私有、公共或合作伙伴应用程序,尽管OAuth1配置的实现和存储取决于实现软件。xeroclient旨在遵守以下关于Xero集成的标准

  1. 遵守PSR-2标准。
  2. 使用现代PHP库,如Guzzle。
  3. 轻量级,可插入到各种框架中,这些框架以自己的方式执行规范化和数据建模。
  4. 可测试。

最终,使用xeroclient的软件需要处理序列化、数据建模、OAuth2重定向工作流程、配置或内容管理。

Build Status

有关向此项目贡献的更多信息(包括行为准则、问责制和如何开始),请参阅CONTRIBUTING

依赖关系

用法

使用OAuth2从Xero API请求访问令牌。

// Create a new provider.
$provider = new \Radcliffe\Xero\XeroProvider([
    'clientId' => 'my consumer key',
    'clientSecret' => 'my consumer secret',
    'redirectUri' => 'https://example.com/path/to/my/xero/callback',
    // This will always request offline_access.
    'scopes' => \Radcliffe\Xero\XeroProvider::getValidScopes('accounting'),
]);

// Gets the URL to go to get an authorization code from Xero.
$url = $provider->getAuthorizationUrl();

使用授权代码创建Guzzle客户端(见上方)

$client = \Radcliffe\Xero\XeroClient::createFromToken('my consumer key', 'my consumer secret', $code, 'authorization_code', 'accounting');
// Store the access token for the next 30 minutes or so if making additional requests.
$tokens = $client->getRefreshedToken();

使用访问令牌创建Guzzle客户端

$client = \Radcliffe\Xero\XeroClient::createFromToken(
	'my consumer key',
	'my consumer secret',
	'my access token',
	null,
	'accounting',
	[],
	[],
	'https://example.com/path/to/my/xero/callback'
);

使用刷新令牌创建Guzzle客户端

访问令牌在30分钟后过期,因此您还可以使用存储的刷新令牌创建新的客户端。

$client = \Radcliffe\Xero\XeroClient::createFromToken(
	'my consumer key',
	'my consumer secret',
	'my request token',
	'refresh_token',
	'accounting',
	[],
	[],
	'https://example.com/path/to/my/xero/callback'
);
// Get the refreshed tokens and store it somewhere.
$tokens = $client->getRefreshedToken();

使用客户端实例进行请求

try {
	$options = [
		'query' => ['where' => 'Name.StartsWith("John")'],
		'headers' => ['Accept' => 'application/json'],
	];
	$response = $client->request('GET', 'Accounts', $options);

	// Or use something like Symfony Serializer component.
	$accounts = json_decode($response->getBody()->getContents());
} catch (\GuzzleHttp\Exception\ClientException $e) {
	echo 'Request failed because of ' . $e->getResponse()->getStatusCode();
}

错误处理

如果配置的客户端没有有效的Xero API URL或未提供auth_token,则在Guzzle请求部分抛出XeroRequestException。

之前XeroClient在实例化时会抛出异常,但这种情况不再存在。如果直接使用initialize方法,XeroClient可能会因其他原因失败。

与旧版OAuth1应用程序一起使用

请参阅0.2分支和版本< 0.3.0。

Xero Helper特性

XeroHelperTrait提供了一些有用的方法,可以附加到您的类中,以处理各种Xero API查询参数和头信息。

许可证

  • 此软件主要在MIT许可证下许可。
  • 特批使用GPLv2许可证使用软件。

其他库

  • xero-php-oauth2提供了一个自动生成的SDK,用于访问Xero API,并将Guzzle注入到每个模型中。
  • xero-php提供了一个基于数据模型假设的一站式解决方案,使用Curl为PHP 5.3应用程序。
  • PHP-Xero提供了OAuth1和Xero类,位于全局命名空间中。非常过时,不应使用。我有分支
  • XeroBundle 提供了一个 Symfony2 扩展包,这是本轻量级库的灵感来源。您可以将自己的工厂类封装起来以忽略 Symfony2 扩展包的配置。目前由于我的错误而无法正常工作,除非您使用 我的分支
  • XeroOAuth-PHP 在全局命名空间中提供了 OAuth1 和 Xero 类,由 Xero API 团队维护,适用于旧版本的 PHP 5.3 应用程序。
  • Xero API 提供了一个 Drupal 模块,可以与 Xero API 集成。在 Drupal 8 中,这依赖于本库或上述 XeroBundle。
  • 还有许多其他库提供了自定义代码。

关联

本库与 Xero Limited 无关联。