• This repository has been archived on 21/Nov/2019
  • Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 13 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP class for the Xero API V2

Warning: This lib is no longer maintained.

Looking for OAuth 2.0?

Please checkout Xero PHP sdk for OAuth 2.0 and it's companion kitchen sync app

Looking for OAuth 1.0a?

Please checkout the community project https://github.com/calcinai/xero-php

XeroOAuth-PHP (DEPRECATED)

This repository has been archived and no further issues or pull requests will reviewed. Feel free to fork the repo and work with the code.

PHP library for working with the Xero OAuth API.

Intro

XeroOAuth-PHP is a sample library for use with the Xero API (http://developer.xero.com). The Xero API uses OAuth 1.0a, but we would not recommend using this library for other OAuth 1.0a APIs as the Xero API has one of the more advanced implementations (RSA-SHA1, etc) and thus has many configuration options not typically used in other APIs.

This library is designed to get a developer up and running quickly with the OAuth authentication layer, but there will be some customisation of its implementation required before it can be used in a production environment.

Requirements

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

Setup

To get setup, you will need to modify the values in the _config.php file to your own requirements and application settings or see the customised example file for each different application type, public.php, private.php or partner.php.

Usage

There are a number of functions used when interacting with Xero:

Make a request

The request function lies at the core of any communication with the API. There are a number of types of requests you may wish to make, all handled by the request() function.

request($method, $url, $parameters, $xml, $format)
Parameters
  • Method: the API method to be used (GET, PUT, POST)
  • URL: the URL of the API endpoint. This is handled by a special function (see below)
  • Parameters: an associative array of parameters such as where, order by etc (see https://developer.xero.com/documentation/api/requests-and-responses/)
  • XML: request data (for PUT and POST operations)
  • Format: response format (currently xml, json & pdf are supported). Note that PDF is not supported for all endpoints

Generate a URL

Create a properly formatted request URL.

url($endpoint, $api)
Parameters
  • Endpoint: the endpoint you wish to work with. Note there are OAuth endpoints such as 'RequestToken' and 'AccessToken' in addition to various API endpoints such as Invoices, Contacts etc. When specifying a resource, such as Invoices/$GUID, you can construct the request by appending the GUID to the base URL.
  • API: there are two APIs: core (core accounting API) and payroll (payroll application API). Default is core.

Parse the response

Once you get data back, you can pass it through the parseResponse function to turn it into something usable.

parseResponse($response, $format)
Parameters
  • Response: the raw API response to be parsed
  • Format: xml pdf and json are supported, but you cannot use this function to parse an XML API response as JSON - must correspond to the requested response format.

Authorise

For public and partner API type applications using the 3-legged OAuth process, we need to redirect the user to Xero to authorise the API connection. To do so, redirect the user to a url generated with a call like this:

url("Authorize", '') . "?oauth_token=".$oauth_token."&scope=" . $scope;
Appendages
  • oauth_token: this is a request token generated in a prior RequestToken call
  • scope: the Payroll API is a permissioned API and required a comma separated list of endpoints the application is requesting access to e.g. $scope = 'payroll.payrollcalendars,payroll.superfunds,payroll.payruns,payroll.payslip,payroll.employees';

Refresh an access token

For partner API applications where the 30 minute access tokens can be programatically refreshed via the API, you can use the refreshToken function:

refreshToken('the access token', 'the session handle')
Parameters
  • Access token: the current access token
  • Session handle: the session identifier handle

Debug

Setup Diagnostics

As you are getting set up, you may run into a few configuration issues, particularly with some of the more advanced application types such as partner.

To make sure your configuration is correct, you can run a diagnostics function:

diagnostics();

This returns an array of error messages (if there are any). These are in human readable form so should be enough to put you on the right track. If not, check the Xero developer centre and forum for more detail.

It would probably be a bad idea to run this in your production code as the errors returned ones only a developer can resolve, not the end user.

Runtime errors

There are many reasons why an error may be encountered: data validation, token issues, authorisation revocation etc. It is important to inspect not just the HTTP response code, but also the associated error string.

A very basic error output function is included in the sample code, which outputs all available information related to an error. It would need to be substantially tidied up before the results could be surfaced in a production environment.

outputError($object);

Response Helpers

Understanding the type of message you are getting from the API could be useful. In each response that is not successful, a helper element is returned:

  • TokenExpired: This means that the access token has expired. If you are using a partner API type application, you can renew it automatically, or if using a public application, prompt the user to re-authenticate
  • TokenFatal: In this scenario, a token is in a state that it cannot be renewed, and the user will need to re-authenticate
  • SetupIssue: There is an issue within the setup/configuration of the connection - check the diagnostics function

TODO

  • Reading a value from a report
  • Better WHERE and ORDER examples
  • Merge OAuthsimple changes for RSA-SHA1 back to parent repo

License & Credits

This software is published under the MIT License.

OAuthSimple

OAuthsimple.php contains minor adaptations from the OAuthSimple PHP class by United Heroes.

tmhOAuth

XeroOAuth class is based on code and structure derived from the tmhOAuth library.

Major change history

0.8 - 16th December 2016

Deprecated Entrust Certificates for Partner Apps by commenting out related code and updating base URL to api.xero.com

0.7 - 1st Feb 2016

PHP7 support via @tomcastleman Moved releases to Github release function

0.6 - 19th April 2015

Added composer support. Modified content-type so is also set for PUT requests

0.5 - 16th November 2014

Added examples for CRU of tracking categories and options. Updated the CA certs to a recent one - warning that if you are using a very old version of curl you may get 'cert invalid' type error. Removed an unused function and tidied up comments on another to make them more sensible.

0.4 - 29th September 2014

Merged some pull requests, addressed an issue with multiple calls having signature validation issues.

0.3 - 3rd January 2014

Merged a number of pull requests, tidied up formatting and extended sample tests.

0.2 - 13th May 2013

Merged to master, added more tests and improved security handling for partner API apps.

0.1 - 10th May 2013

Initial release candidate prepared and released to 'refactor' branch.

More Repositories

1

xero-node

Xero Node SDK for OAuth 2.0 generated from XeroAPI/Xero-OpenAPI
TypeScript
194
star
2

Xero-Net

A skinny wrapper of the Xero API. Supports Payroll, Accounting & Files
C#
129
star
3

xero-python

Official Xero OAuth 2.0 python SDK
Python
122
star
4

Xero-NetStandard

A wrapper of the Xero API in the .NetStandard 2.0 framework. Supports Accounting, Payroll AU/US, and Files
C#
121
star
5

Xero-OpenAPI

An OpenAPI description of the Xero API
Mustache
94
star
6

xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
PHP
90
star
7

Xero-Java

Official Java client for use with Xero API
Java
74
star
8

xero-ruby

Xero Ruby SDK for OAuth 2.0 generated from XeroAPI/Xero-OpenAPI
Ruby
57
star
9

xoauth

A CLI tool for obtaining JWTs from OpenId Connect providers
Go
49
star
10

XeroAPI.Net

(previous SDK version - no longer supported)
C#
47
star
11

xero-node-oauth2-app

NodeJS app for demonstrating the xero-node v4 SDK
TypeScript
35
star
12

xero-python-oauth2-starter

Python
29
star
13

xero-postman-oauth2

A postman collection for use with Xero's API and OAuth 2.0
24
star
14

xerogolang

Golang SDK for the Xero API
Go
24
star
15

xero-php-oauth2-app

PHP app for demonstrating the xero-php-oauth2 SDK
PHP
22
star
16

xero-php-oauth2-starter

This is a starter app with the code to perform OAuth 2.0 authentication
PHP
16
star
17

xero-python-oauth2-app

python app for demonstrating the xero-python SDK
Python
16
star
18

XeroWebhooksReceiver

An example .Net Core application for receiving webhooks from Xero
C#
15
star
19

xero-netstandard-oauth2-starter-dotnet-core

This is a starter app build with .NET Core 3.1 MVC to demonstrate Xero OAuth 2.0 Client Authentication & OAuth 2.0 APIs.
C#
14
star
20

Xero-Postman

A Postman collection for authenticating to the Xero API
13
star
21

xero-ruby-oauth2-app

Ruby on rails app for demonstrating the xero-ruby SDK
Ruby
12
star
22

Xero-Insomnia

Insomnia collection for the [XeroAPI](https://developer.xero.com/documentation/) - plug in your API app params to start exploring
11
star
23

xero-netstandard-oauth2-samples

Contains sample implementations making use of Xero Sign In and Outh2
C#
10
star
24

golang-oauth2-example

A basic example using golang to complete the OAuth 2 flow on Xero's API without the use of an SDK.
Go
9
star
25

xero-netstandard-oauth2-app

The companion app for Xero .NET SDK
C#
8
star
26

node-oauth2-example

A short and simple example using node and express with openid-client to complete the OAuth flow on Xero's OAuth 2 API without the use of an SDK.
JavaScript
8
star
27

Xero-NetStandard-Webhooks-Receiver

C#
7
star
28

xero-node-oauth2-ts-starter

Starter typescript code for use with xero-node v4
TypeScript
7
star
29

xero-java-oauth2-app

Java app for demonstrating the Xero-Java SDK
Java
6
star
30

xero-oauth2-omniauth-strategy

An Omniauth strategy created for Xero API OAuth 2 based on the generic Omniauth OAuth 2 strategy.
Ruby
6
star
31

xero-node-oauth2-react-app

JavaScript
6
star
32

Api.Documentation

Documentation for Xero's public API
6
star
33

xero-netstandard-oauth2-starter-app-dotnet-framework

This is a starter app build with .NET Framework v4.6.1 MVC to demonstrate Xero OAuth 2.0 Client Authentication & OAuth 2.0 APIs.
JavaScript
6
star
34

net-desktop-pkce-example

.NET OAuth 2.0 example using PKCE to interact with the XeroAPI
C#
4
star
35

xero-oauth2-omniauth-sample

A Ruby on Rails sample application to demonstrate the usage of xero-oauth2-omniauth ruby gem.
Ruby
4
star
36

xero-node-sso-app

Showing an example Single Sign On / "Sign up with Xero" Flow using xero-node SDK
EJS
3
star
37

xero-ruby-oauth2-starter

This is a starter app to perform OAuth 2.0 authentication with xero-ruby
Ruby
3
star
38

xero-ruby-sso-form

An example of the "Sign up with Xero to Lead" flow using the Ruby SDK. The authentication results in a pre-populated sign up form.
Haml
3
star
39

xero-php-webhook

PHP example of code for verifying and receiving a Xero webhook
PHP
3
star
40

Xero-Postman-Tutorial-PKCE-Edition

3
star
41

xero-php-oauth2-custom-connections-starter

Starter app showing Xero's Custom Connections functionality aka OA2 grant_type: `client_credentials`
PHP
3
star
42

xero-java-oauth2-starter

Java starter code for use with Xero-Java SDK to complete OAuth 2 flow
Java
2
star
43

xeropracticemanager-dotnetcore-oauth2-sample

Contains sample implementations making use of the Xero Practice Manager v3 API and OAuth2
C#
2
star
44

xero-ruby-token-migration-script

Ruby script for migrating OAuth1.0a tokens to OAuth2.0
Ruby
2
star
45

workflowmax-postman-oauth2

A postman collection for use with WorkflowMax's API and OAuth 2.0
2
star
46

php-oauth2-example

A PHP example of the OAuth 2.0 flow and Xero's API without the use of an SDK
PHP
2
star
47

xero-netstandard-oauth2-blazor-pkce

Sample application showing a basic Files Api integration using Blazor WebAssembly.
C#
2
star
48

xero-ruby-custom-connections-starter

Simple starter repo showing how to integrate with Xero
Ruby
1
star
49

workflowmax-dotnetcore-oauth2-sample

Contains sample implementations making use of the WorkflowMax v3 API and OAuth2
C#
1
star
50

xero-landing-page

A landing page template to help app partners market their new integration in Xero's marketplace
1
star
51

xero-net-oauth2-sampletokenmigration

Sample app for migrating OAuth1.0a tokens to OAuth2.0 tokens.
C#
1
star
52

Xero-Net-Sign-Up-With-Xero-Samples

C#
1
star
53

Xero-NetStandard-custom-connections-starter

Starter app showing Xero's Custom Connections functionality aka OA2 grant_type: `client_credentials`
C#
1
star
54

xero-python-custom-connections-starter

Custom Connections starter app for xero-python and client_credentials grant
Python
1
star