Table of Contents Previous Next

Sugar Developer Guide

Version 5.1


Chapter 2 Application Framework : Web Services : SugarSoap Examples

SugarSoap Examples
Every Sugar installation comes with a series of SugarSoap examples located in the examples/ directory of the application installation. There you will find several examples that go beyond those outlined in this section.
Logging in via SOAP and getting a Sugar user's GUID
<?php
require_once('nusoap/nusoap.php');
$client = new soapclient('http://www.example.com/sugar/soap.php?wsdl',true);
$auth_array = array(
'user_auth' => array(
'user_name' => 'bob',
'password' => md5('mysecretpassword'),
)
);
 
$login_results = $client->call('login',$auth_array);
$session_id = $login_results['id'];
$user_guid = $client->call('get_user_id',$session_id);
printf("\n".$auth_array['user_auth']['user_name'].' has a GUID of ' . $user_guid . "\n\n");
?>
 
Now let’s step this example line by line. Including the NuSOAP code sets up the soapclient object.
require_once('nusoap/nusoap.php');
 
Instantiate the object with two parameters, the Sugar instances WSDL location and a flag that you are passing in a WSDL.
$client = new soapclient('http://www.example.com/sugar/soap.php?wsdl',true);
 
Set up a data structure with the data needed to login.
$auth_array = array(
'user_auth' => array(
'user_name' => 'bob',
'password' => md5('mysecretpassword'),
);
 
We know the previous is needed because of the following WSDL snippets (from http://www.example.com/sugar/soap.php?wsdl):
<message name="loginRequest">
<part name="user_auth" type="tns:user_auth"/>
<part name="application_name" type="xsd:string"/>
</message>
 
---and---
<xsd:complexType name="user_auth">
<xsd:all>
<xsd:element name="user_name" type="xsd:string"/>
<xsd:element name="password" type="xsd:string"/>
<xsd:element name="version" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
 
Here is where we actually send the data. We are calling the 'login' SOAP method
$login_results = $client->call('login',$auth_array);
 
Now we grab a session id number
$session_id = $login_results['id'];
 
We know that we should look in 'id' because of the following WSDL snippet:
<message name="loginResponse">
<part name="return" type="tns:set_entry_result"/>
</message>
 
---and---
 
<xsd:complexType name="set_entry_result">
<xsd:all>
<xsd:element name="id" type="xsd:string"/>
<xsd:element name="error" type="tns:error_value"/>
</xsd:all>
</xsd:complexType>
Now that we have a session ID, we can call another SOAP function, get_user_id:
$user_guid = $client->call('get_user_id',$session_id);
And print out some meaningful output with our findings:
printf("\n".$auth_array['user_auth']['user_name'].' has a GUID of ' . $user_guid . "\n\n");
Inserting a Lead via SOAP calls using PHP
This example includes the PHP code necessary to log into the Sugar installation and insert a new Lead record.
<?php
define('sugarEntry', TRUE);
//Use the NuSOAP files
require_once('nusoap/nusoap.php');
// change the URL here to point to your Sugar installation
$soapclient = new nusoapclient('http://www.example.com/sugar/soap.php?wsdl',true);
// change the user credentials below to be a valid user id and password for the Sugar
// installation defined above
$user_auth = array(
'user_auth' => array(
'user_name' => 'bob',
'password' => md5('mysecretpassword'),
'version' => '0.1'
),
'application_name' => 'soapleadcapture');
 
 
 
$result_array = $soapclient->call('login',$user_auth);
$session_id = $result_array['id'];
 
$user_guid = $soapclient->call('get_user_id',$session_id);
// Up until now, we have not introduced anything new
// The following lines will use the set_entry SOAP call to add
// a Lead from a mixture of POST variables and hard coded
// values, then assign to the authenticated Sugar user...
$set_entry_params = array(
'session' => $session_id,
'module_name' => 'Leads',
'name_value_list'=>array(
array('name'=>'first_name','value'=>$_POST['first_name']),
array('name'=>'last_name','value'=>$_POST['last_name']),
array('name'=>'status', 'value'=>'New'),
array('name'=>'phone_work', 'value'=>$_POST['phone']),
array('name'=>'phone_fax', 'value'=>$_POST['fax']),
array('name'=>'account_name','value'=>$_POST['companyname']),
array('name'=>'lead_source','value'=>'Web Site'),
array('name'=>'description','value'=>$_POST['prod_desc']),
array('name'=>'assigned_user_id', 'value'=>$user_guid)));
 
$result = $soapclient->call('set_entry',$set_entry_params);
// this redirects to a page specified in the previous page...
header("Location: " . $_POST['redirect']);
?>
 
 
Note: it is also possible to create a NuSOAP client as follows without requiring the WSDL:
$ soapclient = new nusoapclient('http://www.example.com/sugar/soap.php’,false);
 
This actually speeds up processing since downloading the WSDL before invoking the method is time intensive.

Table of Contents Previous Next

Copyright 2004-2008 SugarCRM Inc.
Product License