-
Notifications
You must be signed in to change notification settings - Fork 31
SugarClientOverview
The same architecture used for the entire REST PHP Client is used, however the implementation of that architecture applies a few changes to make usage of the SugarAPI Client more intuitive to the flow of Sugar.
The in depth details about how the Client is setup can be found in the Client documentation.
Once the Library is installed in your project by following the instructions in the Installation Guide you can work with the SugarAPI PHP REST Client.
Composer will autoload the entire Client library into the SugarAPI\SDK
Namespace. A default Client is included in the library, which is called SugarAPI, which allows you to pass in your SugarCRM server and authentication credentials upon instantiation, as follows
$instance = 'localhost/sugarcrm/'; $authOptions = array( 'username' => 'user', 'password' => 'pass' ); $SugarAPI = new \SugarAPI\SDK\SugarAPI($instance,$authOptions);
Once the object is setup in code, you will need to set the credentials being used to authenticate to the SugarCRM application.
In the above code, during instantiation the authentication credentials of username and password were passed as the second parameter, which would have configured the SugarAPI Client for authentication. However if they are not passed during instantiation, you can use the setCredentials()
method on the SugarAPI Client to setup the authentication credentials.
See the Authentication page for further details and usage surrounding the Rest Client and authentication to a SugarCRM server.
Once authentication credentials are configured, you can Login to your Sugar application server, by simply calling the login()
method on the SugarAPI Client.
try{ $SugarAPI->login(); }catch(SugarAPI\SDK\Exception\Authentication\AuthenticationException $ex){ //A failed authentication attempt will throw an AuthenticationException with a message on the error returned from the SugarCRM server echo "Failed to authenticate to server. Message ".$ex->getMessage(); }
Once you have authenticated to the SugarCRM application, you can call the various dynamic methods outlined in the Endpoints documentation.
A short example of usage is included below, which showcases how to call the dynamic method which is associated with an Endpoint, and how to pass data to the Endpoint and retrieve the response.
Getting a list of 5 Account records with a filter using the filterRecords()
method:
$FilterEndpoint = $SugarAPI->filterRecords('Accounts'); $data = array( 'max_num' => 5, 'filter' => array( array( '$or' => array( array( //name starts with 'a' "name" => array( '$starts'=>"A", ) ), array( //name starts with 'b' "name" => array( '$starts'=>"b", ) ) ), ), ), ); $response = $FilterEndpoint->execute($data)->getResponse(); //Check for successful response, by checking HTTP return code if ($response->getStatus()=='200'){ echo $response->getBody(); }else{ if ($response->getError() === FALSE){ //Non 200 response code received from SugarCRM application echo $response->getBody(); }else{ //Curl Error occurred echo $response->getError(); } }