Skip to content

Instantly share code, notes, and snippets.

@spinegar
Last active April 29, 2020 22:43
Show Gist options
  • Save spinegar/6544378 to your computer and use it in GitHub Desktop.
Save spinegar/6544378 to your computer and use it in GitHub Desktop.
Using SugarCRM's New RESTful API With Guzzle
<?php
use Guzzle\Common\Event;
use Guzzle\Http\Client;
// specify the REST web service to interact with
$url = 'http://sugarinstance/rest/v10';
// And admin username/password
$username = 'username';
$password = 'password';
$client = new Client($url);
//Authenticate and retrieve an OAuth2 Token
$request = $client->post('oauth2/token', null, array(
'grant_type' => 'password',
'client_id' => 'sugar',
'username' => $username,
'password' => $password,
));
$results = $request->send()->json();
$token = $results['access_token'];
//Register an Event Listener to automatically include
//the OAuth2 token on all requests to the REST web service
$client->getEventDispatcher()->addListener(
'request.before_send', function(Event $event) {
$event['request']->setHeader('OAuth-Token', $token);
}
);
//Retrieve all documents related to the specified account
$request = $client->get('/Accounts/' . $account_id . '/link/documents');
$result = $request->send()->json();
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment