I have been using web services to access Sugar for
sometime now, for purposes of integration and for accessing data from a
portal. While it is reasonably well documented, there are restrictions,
and last week I cam across a requirement I just couldn’t meet with the
standard web services methods. It was time to explore custom web
services.
When I did, I suddenly realised that it was very simple to implement,
and allowed me to push much of the business logic back into the server
where it belongs. I could create a new method that had all sorts of
complex logic,and expose the results via web services.
Now I did think that this might restrict the ability of the client
software; any time it needs to access a Sugar instance, that instance
needs the custom web service installed. By zipping up the
custom web service
as a loadable module (a topic I’ll discuss in the future as it is a
very easy and powerful way to deploy any sort of customisation) this
objection was quickly overcome. In any cases I would normally have
access to the server when doing this type of work.
So, I have included a trivial method here, just to prove that I can. I
think you will see that just about anything can go into such a method,
allowing the web service to provide complex functionality to its client.
Before tackling that, lets see how the standard web service works. We
will look at the version 4.1REST option from Sugar PRO 6.5.13.
service/v4_1/rest.php
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* This is a rest entry point for rest version 4
*/
chdir('../..');
require_once('SugarWebServiceImplv4_1.php');
$webservice_class = 'SugarRestService';
$webservice_path = 'service/core/SugarRestService.php';
$webservice_impl_class = 'SugarWebServiceImplv4_1';
$registry_class = 'registry';
$location = '/service/v4_1/rest.php';
$registry_path = 'service/v4_1/registry.php';
require_once('service/core/webservice.php');
|
You can try this in a browser directly, as follows (your url will be slightly different)
http://localhost/sugarPro6.5.13/service/v4_1/rest.php
This will show you documentation of the standard Sugar methods you can call, and their parameters.
Custom Web Service
Now our custom web service is to reside at custom/service so that it
its upgrade safe. Lets look at the code, and compare it with the
standard Sugar version above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?php
/**
* example of SugarCRM custom web services - server
*
* (c) Copyright Greg Ambrose greg@ambrose.id.au
*
* @version 1.0 20-jun-2013
*
*/
// allow this file to be an entry point
if(!defined('sugarEntry')) define('sugarEntry', true);
// change to entry normal entry point directory
chdir('../../../');
// the name of the class that will inherit from Sugar's SugarRestService
$webservice_class = 'MySugarRestService';
// where this class is defined - though really we can use the original SugarRestService
$webservice_path = 'custom/service/MySugarRestService.php';
// the name of the class that will inherit from Sugar's SugarRestServiceImp
// this will be the class that holds any new methods we want to define for the web service
$webservice_impl_class = 'MySugarRestServiceImpl';
// where this implmentation class resides
$webservice_impl_class_path = 'custom/service/MySugarRestServiceImpl.php';
// variable etc needed by all web services to get started
$registry_class = 'registry';
$location = '/custom/service/rest.php';
$registry_path = 'service/v2_1/registry.php';
require_once('service/core/webservice.php');
?>
|
You will see we just extend the standard classes and create our own.
These are below (MySugarRestService.php and MySugarRestServiceImp.php,
though we could do without the first)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
/**
* OK we didnt need to override this class but here it is anyway
*/
require_once('service/core/SugarRestService.php');
class MySugarRestService extends SugarRestService
{
public function __construct($url)
{
parent::__construct($url);
}
}
?>
|
and MySugarRestServiceImpl.php below. You can see how it extends the Sugar class and adds a new method (getContactDetails).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php
/**
* This is where we add extra methods for custom webservices will reside
*
* (c) Copyright Greg Ambrose greg@ambrose.id.au
*
* @version 1.0 26-jun-2013
*
*/
// get the class we are inheriting from
require_once('service/core/SugarRestServiceImpl.php');
class MySugarRestServiceImpl extends SugarRestServiceImpl
{
/**
* As an example of a new method we will get a specific contact, plus
* the number of documents associated with this contact.
*
* Note we just hard code a reply. We dont even check the session. This will follow later
*
* @param session id
* @param contact id
* @return entry_list values found in an array
*/
function getContactDetails($session, $contactId)
{
$GLOBALS['log']->info('Begin: MySugarWebServiceImpl->getContactDetails');
$outputList = array('ahdhdhdhdh', 'hehgeyyehegegeg');
$GLOBALS['log']->info('End: MySugarWebServiceImpl->getContactDetails');
return array('entry_list'=>$outputList, );
}
}
|
Note that the method just returns hard coded values.
Accessing The Web Service
To prove that it works you need some a client. The following is very
basic code to act as a client calling our new method. In a later post I
will show more examples of client code, but for now this is enough to
get started.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?php
/**
* A script to consume the custom web service we are producing
*
* This is a very basic script taken and modified from Lona Jane
* http://www.lornajane.net/posts/2008/using-curl-and-php-to-talk-to-a-rest-service
*
*/
$serviceUrl = 'http://localhost/sugarPro6.5.13/custom/service/rest.php';
$curl = curl_init($serviceUrl);
// These values are just made up, to show how the calls work
$curlPostData = array(
"method" => 'getContact',
"session" => 'fs635fsg36g',
"contactId" => '6ry477hr47h7',
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPostData);
$curlResponse = curl_exec($curl);
curl_close($curl);
print $curlResponse;
?>
|
When you run that client you get the following response.
|
|
Array
(
[entry_list] => Array
(
[0] => Fred Smith
[1] => Mary Wilson
)
)
|
Which are just the hard coded value set in our server method.
I think that is enough to get started. I haven’t shown the login
process, or how values can be retrieved from Sugar. All will have to
wait for a new post.
http://greg.ambrose.id.au/2013/05/09/creating-custom-web-services-in-sugarcrm/
ReplyDelete