Skip to content
Mike Russell edited this page May 3, 2016 · 14 revisions

#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 Endpont URL, Data, Request and Response.
  • Requests
  • An object for manipulating Curl, without having to dig into Curl intself.
  • Response
  • An object for manipulating the Curl Response

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 details, as follows:

$authCredentials = array(
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'username' => $password,
    'password' => $username,
    'platform' => 'base'
);
$SugarAPI->setCredentials($authCredentials);
Please note that by default the included SugarAPI Client uses the following authentication settings
  • platform = api
  • client_id = sugar
  • client_secret = '' (blank)
It is recommended that you configure a new OAuth Key and Secret for the REST PHP Client in your SugarCRM application, as outlined in the [Support Documentation](https://support.sugarcrm.com/Documentation/Sugar_Versions/7.6/Ent/Administration_Guide/System/#Creating_OAuth_Keys)

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()){
        //Non 200 response code received from SugarCRM application
        echo $response->getBody();
    }else{
       //Curl Error occurred
       echo $response->getError();
    }
}
Clone this wiki locally