1. Go to this page and download the library: Download consilience/xero-api-client 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/ */
consilience / xero-api-client example snippets
use Consilience\XeroApi\Client\Oauth1\Authorise;
// The public certificate for your RSA-SHA1 public/private key pair will
// be registered with the Partner application on the gateway.
// See: https://developer.xero.com/documentation/auth-and-limits/partner-applications
$authoriseClient = new Authorise([
'consumer_key' => 'S8IVZHU6...HUABRRK',
'consumer_secret' => 'PLWK9PBG...VHXAQOH',
'callback_uri' => 'your callback URL where the user will return to',
'redirect_on_error' => true,
'signature_method' => Authorise::SIGNATURE_METHOD_RSA,
'private_key_file' => 'certs/privatekey.pem',
'private_key_passphrase' => '',
]);
$temporaryToken = $authoriseClient->getTemporaryToken();
if ($temporaryToken->isError()) {
throw new Exception(sprintf(
'Failed to get temporary token; error %s (%s)',
$temporaryToken->getErrorCode(),
$temporaryToken->getErrorReason()
));
}
// Store the token object in the session for later.
// JSON serialise it; we will rebuild it.
Session::set('temporary_token', json_encode($temporaryToken));
// Your framework will probably have its own way to do a redirect
// that allows it to exit cleanly.
$authoriseUrl = $authoriseClient->authoriseUrl($temporaryToken);
header('Location: ' . (string)$authoriseUrl);
exit;
// The verifier will be supplied as a GET parameter.
$oauthVerifier = $_GET['oauth_verifier'];
// Retrieve (and rebuild) the temporary token object we saved earlier.
$temporaryToken = new Token(json_decode(Session::get('temporary_token'), true));
// Use these details to get the final Access Token.
$accessToken = $authoriseClient->getAccessToken(
$temporaryToken,
$oauthVerifier
);
use Consilience\XeroApi\Client\Oauth1\Token;
use Consilience\XeroApi\Client\OauthTokenInterface;
// Get the current token details from storage.
$tokenKey = 123;
$accessTokenData = TokenStorage::get($tokenKey);
$accessToken = new Token(json_decode($accessTokenData, true));
// Add a callback to persist any refresh to the token.
$onPersist = function (OauthTokenInterface $accessToken) use ($tokenKey) {
TokenStorage::save($tokenKey, json_encode($accessToken));
};
// Tell the client how to persist token refreshes.
$oauth1Token = $oauth1Token->withOnPersist($onPersist);
// We will add a guard time of five minutes.
// The token will be renewed five minutes early each cycle just to
// cut down on the round-trip API accesses.
$oauth1Token = $oauth1Token->withGuardTimeSeconds(60 * 5);
// Example Private Application consumer key.
$oauth1Token = new Token ([
'oauth_token' => 'PQ4351VSH4FHXTJTPN3JBBBNYSAYXM',
]);
use Consilience\XeroApi\Client\AbstractClient;
use Consilience\XeroApi\Client\App\Partner;
use Consilience\XeroApi\Client\App\AppPrivate;
// This will create a PSR-18 client decorator.
// Just use it like a PSR-18 client.
// $client can be a Psr\Http\Client\ClientInterface PSR-18 client
// of your choice, or `null` for auto-discovery.
$app = new AppPrivate($client, $oauth1Token, [
// or
$app = new Partner($client, $oauth1Token, [
// The key and secret are needed for signing.
'consumer_key' => 'PQ4351VSH4FHXTJTPN3JBBBNYSAYXM',
'consumer_secret' => '1FWE9NCU8SYB8S9ROFDTCUDCC3UXMF',
// RSA is
use Http\Discovery\MessageFactoryDiscovery;
// The request factory is just an example.
// If you have a concrete request class, then just use that.
// We are fetching the organisation from the 2.0 Accounting
// API and requesting a JSON response.
$messageFactory = MessageFactoryDiscovery::find();
// This is a very simple request, with no parameters and no payload.
// Builing more complex requests is a job for another package, and
// that will be auto-generated from the Xero OpenAPI specs.
$request = $messageFactory->createRequest(
'GET',
'https://api.xero.com/api.xro/2.0/organisation'
)->withHeader('Accept', 'application/json');
$response = $app->sendRequest($request);
$payloadData = json_decode((string)$response->getBody(), true);
var_dump($payloadData);