Skip to content

Instantly share code, notes, and snippets.

@Peekmo
Last active August 29, 2015 14:16
Show Gist options
  • Save Peekmo/a6c5d3404f8246b722da to your computer and use it in GitHub Desktop.
Save Peekmo/a6c5d3404f8246b722da to your computer and use it in GitHub Desktop.
{
"require": {
"guzzle/guzzle": "dev-master"
}
}
<pre><?php
require_once 'vendor/autoload.php';
use Guzzle\Http\Client;
use Guzzle\Plugin\Oauth\OauthPlugin;
use Guzzle\Http\Exception\ClientErrorResponseException;
/**
* Configure the following vars by creating a private application in Xero.
*
* @see http://developer.xero.com/api-overview/private-applications/
*/
$key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCD';
$secret = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCD';
$privateKey = 'privatekey.pem';
$client = new Client();
// Configure the OAuth authentication
$client->addSubscriber(new OauthPlugin(array(
'consumer_key' => $key,
'consumer_secret' => $secret,
'token' => $key,
'signature_method' => 'RSA-SHA1',
'signature_callback' => function ($baseString) use ($privateKey) {
$signature = '';
$privateKeyId = openssl_get_privatekey(file_get_contents($privateKey));
openssl_sign($baseString, $signature, $privateKeyId);
openssl_free_key($privateKeyId);
return $signature;
}
)));
// Configure the request to the Organisation endpoint
$request = $client->get('https://api.xero.com/api.xro/2.0/Organisation');
// Send the request to the API and handle the response
try {
$response = $request->send();
} catch (ClientErrorResponseException $ex) {
// Get the error response
$response = $ex->getResponse();
if (0 === strpos($response->getBody(), '<')) {
throw $ex;
} else {
// This is an OAuth error, in url encoded string format
$error = array();
parse_str($response->getBody(), $error);
throw new Exception(sprintf('%s (%s)', $error['oauth_problem_advice'], $error['oauth_problem']));
}
}
echo htmlspecialchars($response->getBody());
?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment