Download the PHP package academe/xero-php without Composer

On this page you can find all versions of the php package academe/xero-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package xero-php

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Table of Contents

XeroPHP API OAuth Access

PHP library for working with the Xero OAuth API.

Intro

This library tackles the following parts of Xero API access:

This package leaves these functions for other packages to handle, thought does coordinate them:

This package differs from the excellent calcinai/xero-php package in the following fundamental ways:

Which package best suites you, will depend on your use-case. Each have pros and cons.

This package needs the OAuth token and secret gained through authorisation to access the API, and the session handler token if automatic refreshing is needed for the Partner Application.

This package does not care what you use at the front end to obtain those tokens. The two packages recommended above to do this are reliable, well documented, and focus on just getting that one job done.

I am mostly focusing on getting this working for the Xero Partner app, as I need a robust librayr that just keeps on running as a scheduled process without losing the tokens and needing a user to re-authenticate. Once a Partner app has been authorised, it should in theory be able to access the Xero account for 10 years, refreshing every 30 minutes. In reality, tokens will get lost - even Xero has downtime that can result in lost authentication tokens.

Areas to Complete (TODO)

Quick Start

Now we have a client to send requests. This is a refreshable client, so if using the Xero Partner app, it will refresh its token automatically when it expires and inform your application via the tokenRefreshCallback.

After sending a request, you can check if the token was refreshed for any action you may want to perform as a result:

If the token is refreshed, then the new token will have been stored by your app through the tokenRefreshCallback.

If you want to refresh the tokens explicitly, before you hit an expired token response, then it can be done like this:

That may be more convenient to do, but be aware that unless you set a guard time, there may be times when you miss an expiry and the request will return an expired token error.

You may want to check that the expiry time is approaching on each run, and renew the token explicitly. This check can be used to see if we have entered a "guard window" preceding the expected expiry time:

The Response Message

Message Instantiation

The ResponseMessage class is instantiated with the response data. Either the Response object or the data extracted from the response can be used to initialise the ResponseMessage:

php if ($result->isCollection()) { $collection = $result->getCollection(); }

if ($result->isResource()) { $resource = $result->getResource(); }

if ($result->isEmpty()) { // No resources - check the metadata to find out why (TODO). }

There may be further collections of resources deeper in the data, such as a list of addresses for a contact.

Response Dates and Times

An attempt is made to convert all dates and times to a Carbon datetime. Xero mixes quite a number of date formats across its APIs, so it is helpful to get them all normallised. Formats I've found so far:

I'm sure there will be more. These fields are recognised solely through the suffix to their name at present. Suffixes recognised are:

Pagination

There is no automatic pagination feature (automatically fetching subsequent pages) when iterating over a paginated resource. A decorator class could easily do this though, and that may make a nice addition to take the logic of "fetching all the matching things" that span more than one page away from the application (ideally the application would make a query, then loop over the resources and each page would be lazy-loaded into the collection automatically when going off the end of the page).

All other datatypes will be either a scalar the API supplied (string, float, int, boolean) or another ResponseData object containing either a single Resource (e.g. "Invoice") or a ResourceCollection (e.g. "CreditNotes").

Resource Properties

Accessing properties of a resource object is case-insensitive. This decision was made due to the mixed use of letter cases throughout the Xero APIs.

A resource will have properties. Each property may be another resource, a resource collection, a date or time, or a scalar (string, integer, float).

Accessing a non-existant property will return an empty Resource. Drilling deeper into an empty resource will give you further empty resources.

But do be aware that when you hit a scalar (e.g. a string) then that is what you will get back and not a Resource object.

The API sometimes returns a null for a field or resource rather than simply omitting the field. Examples are the pagination field when fetching a single payrun, or the problem field when there is no problem. In this case, when you fetch the value, you will be given an empty Resource object instead.

Guzzle Exceptions

By default, the Guzzle client will throw an exception if it receives a non 20x HTTP response. When this happens, the HTTP response can be found in the exception.

Handling non-20x messages like this may not be convenient, so Guzzle can be told not to throw an exception using the exceptions option:

Note: this package now switches off Guzzle exceptions by defaul. You can turn them back on using this parameter if that is desirable. Token refreshing will work both with or without excpetions being enabled.

This option can be used on each request, or set as the default in the ClientProvider instantiation options. This package is designed not to care which approach you take. However, not throwing an exception often makes sense, because even non-20x responses nearly always contain a response body with information the application is going to need to log or to make a decision.

Catching Errors

There are numerous sources of error, and these are reported in many different ways, and with different data structures. The aim of this package will be to try to normalise them, but in the meantime here is a list of those we know:

The places where error details cna be found are:

API Response Structures

Each response will be in one of a number of structures. The structures listed below have been identified so far, with the aim that all will be recognised automatically and normalised to a single resource or collection.

A: Single metadata header; single resource

The resource is in a single node, usually named after the resource content, but not always. Examples include fetching a single Payrun in the GB Payroll v2.0 API.

Response Format A

B: Single metadata header; collection of resources

The resources are in an array under a single node, usually named after the resource content, but not always. Examples include fetching a multiple Payruns in the GB Payroll v2.0 API. Paging metadata is in an object of its own.

Response Format B

C: Single metadata header; collection of a single resource

Some APIs will return both a single resource and multiple resources in an array, with no paging metdata. Distinguishing between the response to requesting a single resource, or matching a single resource in a resource collection, is not possible; the response looks the same withou looking at more details of the content of the resource. Examples include fetching a single payment from the Accounting v2.0 API.

Response Format C

D: Single metadata header; collection of resources

This structure includes some metadata at the root node, including paging details that are not wrapped into an object. Examples include fetching a multiple files from the Files v1.0 API.

Response Format D

E: Array of resources

Some APIs will return an array of resources with no metadata at all, no paging details, no source details.

Response Format E

F: Single resources

Similar to format E, the response contains a single resource, not wrapped into a field or object, and with no metadata to provide context.

Response Format F

There are a number of different formats used to deliver error messages and exceptions (at least four different structures). These will be documented shortly, as they need to be handled using the same rules.

I suspect at least two of these stcutures can be merged into one.

Other Notes

I have noticed the occassional 401 which then works on a retry. Using the Guzzle retry handler would be a good move to avoid unnecessary errors when processing large amounts of data.


All versions of xero-php with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6.0
guzzlehttp/oauth-subscriber Version 0.3.*
nesbot/carbon Version ^1.20
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package academe/xero-php contains the following files

Loading the files please wait ....