PHP code example of mradcliffe / xeroclient

1. Go to this page and download the library: Download mradcliffe/xeroclient 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/ */

    

mradcliffe / xeroclient example snippets


// Create a new provider.
$provider = new \Radcliffe\Xero\XeroProvider([
    'clientId' => 'my consumer key',
    'clientSecret' => 'my consumer secret',
    'redirectUri' => 'https://example.com/path/to/my/xero/callback',
    // This will always request offline_access.
    'scopes' => \Radcliffe\Xero\XeroProvider::getValidScopes('accounting'),
]);

// Gets the URL to go to get an authorization code from Xero.
$url = $provider->getAuthorizationUrl();

$client = \Radcliffe\Xero\XeroClient::createFromToken('my consumer key', 'my consumer secret', $code, 'authorization_code', 'accounting');
// Store the access token for the next 30 minutes or so if making additional requests.
$tokens = $client->getRefreshedToken();

$client = \Radcliffe\Xero\XeroClient::createFromToken(
	'my consumer key',
	'my consumer secret',
	'my access token',
	null,
	'accounting',
	[],
	[],
	'https://example.com/path/to/my/xero/callback'
);

$client = \Radcliffe\Xero\XeroClient::createFromToken(
	'my consumer key',
	'my consumer secret',
	'my request token',
	'refresh_token',
	'accounting',
	[],
	[],
	'https://example.com/path/to/my/xero/callback'
);
// Get the refreshed tokens and store it somewhere.
$tokens = $client->getRefreshedToken();

try {
	$options = [
		'query' => ['where' => 'Name.StartsWith("John")'],
		'headers' => ['Accept' => 'application/json'],
	];
	$response = $client->request('GET', 'Accounts', $options);

	// Or use something like Symfony Serializer component.
	$accounts = json_decode($response->getBody()->getContents());
} catch (\GuzzleHttp\Exception\ClientException $e) {
	echo 'Request failed because of ' . $e->getResponse()->getStatusCode();
}