-
Notifications
You must be signed in to change notification settings - Fork 4
Home
This project follows a structured approach for interacting with the SugarCRM REST API, organized into three major components which are detailed in the architecture page.
The SugarCRM PHP Rest Client is built as a composer package and can be used in two ways:
- On its own
composer require sugarcrm-developers/php-rest-client
- Inside a PHP Projects composer.json File
require: { "sugarcrm-developers/php-rest-client": "1.*" }
Once composer retrieve's the package it will autoload the Sugarcrm\REST
namespace to correspond to the src/
directory of the Repository, so you can being using the framework in your development.
Once the Library is installed in your project you can work with the SugarCRM PHP REST Client.
Composer will autoload the entire Client library into the Sugarcrm\REST
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
$server = 'localhost'; $credentials = array( 'username' => 'admin', 'password' => 'asdf', 'platform' => 'base' ); $SugarAPI = new \Sugarcrm\REST\Client\SugarApi($server, $credentials);
Once the object is setup in code, you will need to set the credentials being used to authenticate to the Sugar application. In the above code, during instantiation the authentication credentials of username and password were passed as the second parameter as well as identifying the platform making the call, which would have configured the SugarAPI Client for authentication.
Once authentication credentials are configured, you can Login to your Sugar application server, by simply calling the login()
method on the SugarAPI Client.
try { if ($SugarAPI->login()) { echo "Logged In: "; } else { echo "Could not login."; pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); } } catch (Exception $ex) { echo "Error Occurred: "; pre($ex->getMessage()); pre($ex->getTraceAsString()); }
We also provide the method isAuthenticated
as a single method to check for cached authentication, attempt refresh of auth, and attempt logging in as our recommended usage with the client.
Once you have authenticated to the SugarCRM application, you can call the various dynamic methods exemplified in the examples/
folder.
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.
$SugarAPI = new \Sugarcrm\REST\Client\SugarApi($server, $credentials); try { if ($SugarAPI->isAuthenticated()) { echo "Logged In: " . json_encode($SugarAPI->getAuth()->getToken(),JSON_PRETTY_PRINT) . "\n"; $Accounts = $SugarAPI->list('Accounts'); $Accounts->filter()->and() ->or() ->starts('name', 's') ->contains('name', 'test') ->endOr() ->equals('assigned_user_id', 'seed_max_id') ->endAnd(); echo "Filtering Accounts that are assigned to User Max, and that either start with an S or contain 'test' in the name: \n" . json_encode($Accounts->filter()->compile()); // Run count API with filter $Accounts->count(); echo "Found " . $Accounts->getTotalCount() . " Accounts.\n"; // Run filter request $Accounts->fetch(); echo json_encode($Accounts->toArray(), JSON_PRETTY_PRINT) . "\n"; //Clear out Collection $Accounts->clear(); //Reset Filter definition $Accounts->filter(true); $Accounts->filter()->or()->date('date_entered') ->between(["2019-01-01", "2019-02-01"]) ->endDate() ->date('date_entered') ->last7Days() ->endDate() ->endOr(); echo "Filtering Accounts that are created between dates, or in the last 7 days: " . json_encode($Accounts->filter()->compile(),JSON_PRETTY_PRINT) . "\n"; $Accounts->fetch(); echo "Accounts: " . json_encode($Accounts->toArray(), JSON_PRETTY_PRINT) . "\n"; } else { echo "Could not login."; $oauthEndpoint = $SugarAPI->getAuth()->getActionEndpoint('authenticate'); $response = $oauthEndpoint->getResponse(); if ($response) { $statusCode = $oauthEndpoint->getResponse()->getStatusCode(); echo "[$statusCode] - " . $oauthEndpoint->getResponse()->getBody()->getContents(); } } } catch (Exception $ex) { echo "Exception Occurred: " . $ex->getMessage(); echo $ex->getTraceAsString(); }
As of right now, version 1.x is being maintained, and in order for the SugarCRM PHP Rest Client to work well for all, version 1.x will not change in the overarching usage of architecture and the usage of the Sugar Client. That being said, if a Bug is found when using the Sugar REST PHP Client please submit an issue, and if you have a solution, submit a Pull Request for that issue. We will try to resolve Bugs as quickly as possible, and release a new version with those Bug fixes.
As far as adding functionality goes, if you would like other Endpoints that are not already included in the Sugar REST PHP Client, we would be more than happy to add them. What we will not do with version 1.x, is alter the current usages of Endpoints (such as their dynamic function names), as we want to maintain backwards compatibility with any code that may be using the framework. You can submit an feature requests via the Github Issues page as well.
Lastly, if you see any documentation that is incorrect in the Wiki, please submit an issue. Unfortunately the Wiki is not open to public editing, however if you submit an issue, and you are proposing a change to the documentation, you can do the following:
- Pull the wiki down via Git
- Sync to your own fork
- Make your changes to the documentation
- Include a link to your Fork's updated Wiki page in the issue