Download the PHP package kgdiem/xero-php without Composer
On this page you can find all versions of the php package kgdiem/xero-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kgdiem/xero-php
More information about kgdiem/xero-php
Files in kgdiem/xero-php
Package xero-php
Short Description Upstream version of calcinai/xero-php -- A client implementation of the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
License
Informations about the package xero-php
XeroPHP
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:
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):
Load a collection of objects and loop through them
Load collection of objects, for a single page, and loop through them (Why?)
Search for objects meeting certain criteria
Load something by its GUID
Or create & populate it
Save it
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 .
From v1.2.0+, Xero context can be injected directly when creating the objects themselves, which then exposes the 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.
Nested objects
Attachments
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 method, which returns the raw content of the PDF. Currently this is limited to Invoices and CreditNotes.
Refer to the 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.
Validating Webhooks
To ensure the webhooks are coming from Xero you should validate the incoming request header that Xero provides.
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\UnauthorizedException |
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.