PHP code example of sudiptpa / xero-php-sdk
1. Go to this page and download the library: Download sudiptpa/xero-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sudiptpa / xero-php-sdk example snippets
use Sujip\Xero\Auth\InMemoryTokenRepository;
use Sujip\Xero\Xero;
$manager = Xero::oauth2(
clientId: 'client-id',
clientSecret: 'client-secret',
redirectUri: 'https://example.com/xero/callback',
)->manager(new InMemoryTokenRepository());
$url = $manager->authorizationUrl(
scopes: ['openid', 'offline_access', 'accounting.contacts'],
state: 'csrf-token',
);
$token = $manager->exchange($code);
$tenants = $manager->connections();
$connected = $manager->connectTenant($tenants[0]->getTenantId());
$contacts = $connected->tenant()
->accounting()
->contacts()
->page(1)
->get();
$connected = $manager->exchangeAndConnect($code, 'tenant-id');
use Sujip\Xero\Xero;
$xero = Xero::withAccessToken('token')
->tenant('tenant-id');
$contacts = $xero->accounting()
->contacts()
->where('Name.Contains(:name)', name: 'Acme')
->orderBy('Name')
->page(1)
->get();
$page = $xero->accounting()
->contacts()
->paginate(page: 2);
use Sujip\Xero\Accounting\Invoice\Invoice;
use Sujip\Xero\Accounting\Contact\Contact;
use Sujip\Xero\Accounting\Invoice\LineItem;
$invoice = $xero->accounting()
->invoices()
->create()
->using(
(new Invoice())
->setType('ACCREC')
->setStatus('DRAFT')
->setContact(
(new Contact())
->setContactID('contact-id')
)
->setReference('PO-1001')
->addLineItem(
(new LineItem())
->setDescription('Consulting')
->setQuantity(2)
->setUnitAmount(150)
)
)
->save();
use Sujip\Xero\Accounting\Account\Account;
use Sujip\Xero\Accounting\Payment\Payment;
$payment = $xero->accounting()
->payments()
->create()
->using(
(new Payment())
->setInvoiceID('invoice-id')
->setAccount(
(new Account())
->setAccountID('account-id')
)
->setDate('2026-03-25')
->setAmount(150)
->setReference('PAY-1001')
)
->save();
use Sujip\Xero\Accounting\Contact\Contact;
$updated = $xero->accounting()
->contacts()
->update('contact-id')
->using(
(new Contact())
->setContactID('contact-id')
->setName('Acme Holdings Pty Ltd')
)
->save();
$attachment = $xero->accounting()
->invoices()
->attachments('invoice-id')
->upload('invoice.pdf', $pdfBinary)
->mimeType('application/pdf')
->
$file = $xero->files()
->upload('contract.pdf', $binary)
->mimeType('application/pdf')
->toFolder('folder-id')
->save();
$fileName = $file->getName();
$folder = $xero->files()
->folders()
->inbox();
$isInbox = $folder?->getIsInbox();
$files = $xero->files()
->forObject('invoice-id')
->get();
$assets = $xero->assets()
->status('registered')
->orderBy('AssetName')
->filterBy('MacBook')
->get();
$assetName = $assets->first()?->getAssetName();
$project = $xero->projects()
->create()
->title('Website rebuild')
->contact('contact-id')
->estimateAmount(1200)
->save();
$projectId = $project->getProjectId();
$entries = $xero->projects()
->timeEntries('project-id')
->user('user-id')
->task('task-id')
->states('INPROGRESS')
->get();
$employees = $xero->payroll()
->au()
->employees()
->page(1)
->get();
$leave = $xero->payroll()
->au()
->leaveApplications()
->create()
->employee('employee-id')
->leaveType('leave-type-id')
->title('Annual Leave')
->startDate('2026-04-01')
->endDate('2026-04-02')
->save();
$timesheet = $xero->payroll()
->nz()
->timesheets()
->create()
->employee('employee-id')
->startDate('2026-03-23')
->endDate('2026-03-29')
->status('Draft')
->save();
$balances = $xero->payroll()
->uk()
->employees()
->find('employee-id')
?->leaveBalances();
$balanceSheet = $xero->finance()
->statements()
->balanceSheet(new DateTimeImmutable('2026-03-31'));
$subscription = $xero->appStore()
->subscriptions()
->find('subscription-id');
$connections = Xero::withAccessToken($token)
->identity()
->connections()
->get();
$verifier = Xero::webhookVerifier($signingKey);
$verifier->assertValid($rawPayload, $signatureHeader);
$webhook = $verifier->parse($rawPayload);
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use Sujip\Xero\Exceptions\TransportException;
use Sujip\Xero\Http\Request;
use Sujip\Xero\Http\Response;
use Sujip\Xero\Http\Transport;
use Sujip\Xero\Xero;
final class GuzzleTransport implements Transport
{
public function __construct(
private readonly GuzzleClient $client = new GuzzleClient()
) {
}
public function send(Request $request): Response
{
try {
$response = $this->client->request($request->method, $request->url(), [
'headers' => $request->headers,
'json' => $request->json,
'body' => $request->body,
]);
} catch (GuzzleException $exception) {
throw new TransportException($exception->getMessage(), previous: $exception);
}
return new Response(
$response->getStatusCode(),
array_map(
static fn (array $values): string => $values[0] ?? '',
$response->getHeaders()
),
(string) $response->getBody(),
);
}
}
$xero = Xero::withAccessToken('token', new GuzzleTransport())
->tenant('tenant-id');
use Sujip\Xero\Auth\InMemoryTokenRepository;
use Sujip\Xero\Xero;
$manager = Xero::oauth2(
clientId: 'client-id',
clientSecret: 'client-secret',
redirectUri: 'https://example.com/xero/callback',
)->manager(new InMemoryTokenRepository());
$url = $manager->authorizationUrl(
scopes: ['openid', 'offline_access', 'accounting.contacts'],
state: 'csrf-token',
);
$manager->exchange($code);
$connected = $manager->connectTenant('tenant-id');
$xero = $connected->tenant();
bash
composer