If you’re new to SugarCRM you’ve probably found it to be a little buggy–maybe even a little bit confounding to modify to your needs. One thing that you’ll inevitably need in this time of integrating web services across websites, companies, platforms and devices is the ability to send data from a web form into SugarCRM. You can use SOAP for this, and it works well, for the most part. But you’ll find there are very few good examples out there on the web to really clarify and demystify a clean SugarCRM/SOAP implementation.
The best example I have found is here: http://www.lornajane.net/posts/2008/SugarCRM-SOAP-API-Examples. LornaJane’s examples were an excellent start, and I thought I’d go ahead and provide you some more details. The upside of these examples is that you won’t need to install nusoap in order for them to work.
Setup: LAMP web server running the latest version of WordPress with tons of plugins and customization. I also use cForms. It’s not important that you are using WordPress or that you have cForms installed. I just happen to like cForms, and I think it’s a fun challenge to integrate all these different systems. If you do use WP and cForms, just follow the instructions in the cForms documentation for customizing my-functions.php in your plugins/cforms-custom directory. The only real difference will be that you will use $POSTdata['varname'] instead of the regular $_POST['varname']. You’ll also need to use stripslashes() and pay attention to the variable names that cForms passes. I won’t be going into the specific details of cForms-to-SugarCRM here, but you should be able to infer how to implement that from this explanation.
This example uses a simple web form POST calling a PHP document as an action. For simplicity, let’s assume you have a web form with the following fields: first_name, last_name, email_address, phone_number and message.
When you submit the form, it calls “form-action.php”. You will implement the examples here in this PHP file.
Getting Started
To get started, let’s just get a basic example going. Copy and paste the following code in your form-action.php document. If you want to look at the SugarCRM documentation for these SOAP calls, go here: http://sugarcrm.openapp.org/2006/04/24/using-sugarcrm-soap/ Part of the reason I’m writing this post is because they don’t provide any examples for these calls. Once you get the hang of it though, that link will be quite helpful to you.
<?php
// set up options array
$options = array(
“location” => ‘http://your.website.here/sugarcrm/soap.php’,
“uri” => ‘http://www.sugarcrm.com/sugarcrm’,
“trace” => 1
);//user authentication array
$user_auth = array(
“user_name” => ‘YOUR_SUGAR_USERNAME’,
“password” => MD5(’YOUR_SUGAR_PASSWORD’),
“version” => ‘.01′
);// connect to soap server
$client = new SoapClient(NULL, $options);// Login to SugarCRM
$response = $client->login($user_auth,’test’);$session_id = $response->id;
// look what modules sugar exposes
$response = $client->get_available_modules($session_id);// look in more detail at the fields in a module
//$response = $client->get_module_fields($session_id, ‘Accounts’);var_dump($response);
?>
So what’s going on here? Your options array is storing some variables that you’ll use a few lines later in the SoapClient call. The options array is where you specify the connection parameters to the server that has your SugarCRM installation. Naturally, you’ll want to make sure you enable port 80 access through your router/firewall because that’s the default port for HTTP and also SOAP. The next array of interest is $user_auth, which is where you specify your user name, password and SOAP version. After that you pass the connection options into the SoapClient() call and then log in via the login() call. The ‘test’ parameter is a required application name, which can be most anything you want to call it. Once you pass the authentication, the $session_id variable grabs the session id for your current SOAP connection. You’ll need this with most, if not all, calls that you make from this point forward.
The next few lines of code are the fun part. The “$response = $client->get_available_modules($session_id);” line is grabbing all of the modules in your SugarCRM installation. This is very useful, for instance, when you want to know if the particular module you are attempting to write to is exposed. It’s also helpful when you’re trying to debug your SOAP calls and you’ve used the wrong module name, and so on. Below that is another example call that, if you uncomment the line, will return all of the fields that are exposed in a specific module, in this example–Accounts. Finally, var_dump($response) echos out whatever your $response variable has collected above.
Play around with it and get a feel for how it works. Notice that any custom modules you create show up in the list of available modules. Notice also that some modules, like Users, don’t return any available fields at all.