-
Notifications
You must be signed in to change notification settings - Fork 31
Home
#SugarCRM REST PHP Client
##Architecture There are four main components to the PHP REST Client
- Client
- The main object used to make calls to the API. It authenticates and manages authentication to the API throughout the usage of the Object lifespan.
- Endpoint
- A specific Endpoint of the API. Manages the Endpoint URL, Data, Request and Response.
- Requests
- An object for manipulating Curl, without having to dig into Curl itself.
- Response
- An object for manipulating the Curl Response, and providing a way to customize and process the data received in a meaningful way.
These four areas are designated by the folder structure in the src/
directory of the repository.
##Basic Usage
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.
###Instantiation
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);
###Authentication
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 SugarCRM 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(); }
##Using Endpoints Once you have authenticated to the SugarCRM application, you can call the various 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 /filter POST API Endpoint:
$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(); } }