Extending v1 - v4.1 Web Services
Overview
The guide will demonstrate how to add your own custom methods to the REST and SOAP API or extend existing ones. It is important to note that this customization is not supported on Sugar's cloud service and it is recommended to extend the latest endpoints instead.
Extending the API
The following example will demonstrate how to extend the v4_1 API.
Defining the Entry Point Location
This is where you define the directory that will contain your new REST and SOAP entry points. We recommend a path formatted as follows:
./custom/service/{version}_custom/
The actual location of the entry points does not matter, however, using a path such as this will allow you to call your entry points as follows:
- http://{sugar_url}/custom/service/{version}_custom/rest.php
- http://{sugar_url}/custom/service/{version}_custom/soap.php
Define the SugarWebServiceImpl Class
The next step is to define a new SugarWebServiceImpl
class. Since we are using v4_1, we need to extend ./service/v4_1/SugarWebServiceImplv4_1.php
and add our new method. To do this, we will create the file:
./custom/service/v4_1_custom/SugarWebServiceImplv4_1_custom.php
<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
require_once('service/v4_1/SugarWebServiceImplv4_1.php');
class SugarWebServiceImplv4_1_custom extends SugarWebServiceImplv4_1
{
/*
* Returns the session id if authenticated
*
* @param string $session
* @return string $session - false if invalid.
*
*/
function example_method($session)
{
$GLOBALS['log']->info('Begin: SugarWebServiceImplv4_1_custom->example_method');
$error = new SoapError();
//authenticate
if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error))
{
$GLOBALS['log']->info('End: SugarWebServiceImplv4_1_custom->example_method.');
return false;
}
return $session;
}
}
?>
Define the Registry Class
Next, we will define the registry class that will register our new function. This file will be located at:
./custom/service/v4_1_custom/registry.php
<?php
require_once('service/v4_1/registry.php');
class registry_v4_1_custom extends registry_v4_1
{
protected function registerFunction()
{
parent::registerFunction();
$this->serviceClass->registerFunction('example_method', array('session'=>'xsd:string'), array('return'=>'xsd:string'));
}
}
?>
Define the REST Entry Point
This REST entry point will be located at:
./custom/service/v4_1_custom/rest.php
<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
chdir('../../..');
require 'include/entryPoint.php';
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_path = 'service/core/SugarRestService.php';
$webservice_class = 'SugarRestService';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$registry_class = 'registry_v4_1_custom';
$location = 'custom/service/v4_1_custom/rest.php';
require_once('service/core/webservice.php');
?>
Define the SOAP Entry Point
This SOAP entry point will be located at:
./custom/service/v4_1_custom/soap.php
<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
chdir('../../..');
require 'include/entryPoint.php';
require_once('SugarWebServiceImplv4_1_custom.php');
$webservice_class = 'SugarSoapService2';
$webservice_path = 'service/v2/SugarSoapService2.php';
$webservice_impl_class = 'SugarWebServiceImplv4_1_custom';
$registry_class = 'registry_v4_1_custom';
$registry_path = 'custom/service/v4_1_custom/registry.php';
$location = 'custom/service/v4_1_custom/soap.php';
require_once('service/core/webservice.php');
?>