承接 pedalme/xero-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pedalme/xero-php

Composer 安装命令:

composer require pedalme/xero-php

包简介

A client implementation of the Xero API, with a cleaner OAuth interface and ORM-like abstraction.

README 文档

README

Build Status Latest Stable Version Total Downloads

A client library for the Xero API, including an OAuth interface and ORM-like abstraction.

This is loosely based on the functional flow of XeroAPI/XeroOAuth-PHP, but is split logically into more of an OO design.

This library has been tested with Private, Public and Partner applications.

Requirements

  • PHP 5.5+
  • php_curl extension - ensure a recent version (7.30+)
  • php_openssl extension

Setup

Using composer:

composer require calcinai/xero-php

Otherwise just download the package and add it to your autoloader. Namespaces are PSR-4 compliant.

Usage

All the examples below refer to models in the XeroPHP\Models\Accounting namespace. Additionally, there are models for PayrollAU, PayrollUS, Files, and Assets

Create a XeroPHP instance (sample config included):

$xero = new \XeroPHP\Application\PrivateApplication($config);

Load a collection of objects and loop through them

$contacts = $xero->load(Contact::class)->execute();

foreach ($contacts as $contact) {
    print_r($contact);
}

Load collection of objects, for a single page, and loop through them (Why?)

$contacts = $xero->load(Contact::class)->page(1)->execute();

foreach ($contacts as $contact) {
    print_r($contact);
}

Search for objects meeting certain criteria

$xero->load(Invoice::class)
    ->where('Status', Invoice::INVOICE_STATUS_AUTHORISED)
    ->where('Type', Invoice::INVOICE_TYPE_ACCREC)
    ->execute();

Load something by its GUID

$contact = $xero->loadByGUID(Contact::class, $guid);

Or create & populate it

$contact = new Contact($xero);

$contact->setName('Test Contact')
    ->setFirstName('Test')
    ->setLastName('Contact')
    ->setEmailAddress('test@example.com');

Save it

$contact->save();

If you have created a number of objects of the same type, you can save them all in a batch by passing an array to $xero->saveAll().

From v1.2.0+, Xero context can be injected directly when creating the objects themselves, which then exposes the ->save() method. This is necessary for the objects to maintain state with their relations.

Saving related models

If you are saving several models at once, by default additional model attributes are not updated. This means if you are saving an invoice with a new contact, the contacts ContactID is not updated. If you want the related models attributes to be updated you can pass a boolean flag with true to the save method.

$xero->save($invoice, true);

Nested objects

$invoice = $xero->loadByGUID(Invoice::class, '[GUID]');
$invoice->setContact($contact);

Attachments

$attachments = $invoice->getAttachments();
foreach ($attachment as $attachment) {
    //Do something with them
    file_put_contents($attachment->getFileName(), $attachment->getContent());
}

//You can also upload attachemnts
$attachment = Attachment::createFromLocalFile('/path/to/image.jpg');
$invoice->addAttachment($attachment);

To set the IncludeOnline flag on the attachment, pass true as the second parameter for ->addAttachment().

PDF - Models that support PDF export will inherit a ->getPDF() method, which returns the raw content of the PDF. Currently this is limited to Invoices and CreditNotes.

Refer to the examples for more complex usage and nested/related objects. There's also a sample PHP app using this library.

Webhooks

If you are receiving webhooks from Xero there is Webhook class that can help with handling the request and parsing the associated event list.

$webhook = new Webhook($application, $request->getContent());

/**
 * @return int
 */
$webhook->getFirstEventSequence();

/**
 * @return int
 */
$webhook->getLastEventSequence();

/**
 * @return \XeroPHP\Webhook\Event[]
 */
$webhook->getEvents();

See: Webhooks documentation

Validating Webhooks

To ensure the webhooks are coming from Xero you should validate the incoming request header that Xero provides.

if (! $webhook->validate($request->headers->get('x-xero-signature'))) {
    throw new Exception('This request did not come from Xero');
}

See: Signature documentation

Handling Errors

Your request to Xero may cause an error which you will want to handle. You might run into errors such as:

  • HTTP 400 Bad Request by sending invalid data, like a malformed email address.
  • HTTP 503 Rate Limit Exceeded by hitting the API to quickly in a short period of time.
  • HTTP 400 Bad Request by requesting a resource that does not exist.

These are just a couple of examples and you should read the official documentation to find out more about the possible errors.

Thrown exceptions

This library will parse the response Xero returns and throw an exception when it hits one of these errors. Below is a table showing the response code and corresponding exception that is thrown:

HTTP Code Exception
400 Bad Request \XeroPHP\Remote\Exception\BadRequestException
401 Unauthorized \XeroPHP\Remote\Exception\UnauthorizedException
403 Forbidden \XeroPHP\Remote\Exception\ForbiddenException
403 ReportPermissionMissingException \XeroPHP\Remote\Exception\ReportPermissionMissingException
404 Not Found \XeroPHP\Remote\Exception\NotFoundException
500 Internal Error \XeroPHP\Remote\Exception\InternalErrorException
501 Not Implemented \XeroPHP\Remote\Exception\NotImplementedException
503 Rate Limit Exceeded \XeroPHP\Remote\Exception\RateLimitExceededException
503 Not Available \XeroPHP\Remote\Exception\NotAvailableException
503 Organisation offline \XeroPHP\Remote\Exception\OrganisationOfflineException

See: Response codes and errors documentation

Handling exceptions

To catch and handle these exceptions you can wrap the request in a try / catch block and deal with each exception as needed.

try {
    $xero->save($invoice);
} catch (NotFoundException $exception) {
    // handle not found error
} catch (RateLimitExceededException $exception) {
    // handle rate limit error
}

See: Working with exceptions

pedalme/xero-php 适用场景与选型建议

pedalme/xero-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pedalme/xero-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pedalme/xero-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 pedalme/xero-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 251
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-25