|
/Coding/php:
A Simple SugarCRM API Class
This should give anyone needing to use the SugarCRM API a quickstart:
* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ ?> 'https://url.of.your.sugarcrm.installation.com/sugarcrm/soap.php', "uri" => 'http://www.sugarcrm.com/sugarcrm', "trace" => 1 ); //user authentication array (version is SOAP version) private $user_auth = array(); function __construct() { // must use constructor b/c of MD5 function call $this->user_auth = array( "user_name" => 'sugaruserid', "password" => MD5('yourpasswd'), "version" => '.01' ); } // arrays for input into sugarCRM modules var $contact = array(); var $account = array(); function login() { $this->client = new SoapClient(NULL, $this->options); $response = $this->client->login($this->user_auth); // var_dump($response); // just in case you need it $this->session_id = $response->id; printf("session id = ". $this->session_id); $this->user_id = $this->client->get_user_id($this->session_id); printf(" " . $this->user_auth['user_name'] . ' has a GUID of ' . $this->user_id . "
"); } function getModules() { $response = $this->client->get_available_modules($this->session_id); foreach ($response->modules as $i => $value) { printf($response->modules[$i] . "
"); } } function getModuleFields($module) { $response = $this->client->get_module_fields($this->session_id, $module); printf("Module " . $response->module_name .' has the following fields:' . "
"); printf("
'); } function setContactField($fieldname, $fieldvalue) { $this->contact[] = array("name" => $fieldname,"value" => $fieldvalue); } function storeContact() { // into sugar's Contacts database $response = $this->client->set_entry($this->session_id, 'Contacts', $this->contact); var_dump($response); } } ?>
" . " "); foreach ($response->module_fields as $i => $value) { printf('Name Type Label " . "Required Options " . "'); } printf(' ' . $response->module_fields[$i]->name . ' ' . '' . $response->module_fields[$i]->type . ' ' . '' . $response->module_fields[$i]->label . ' ' . '' . $response->module_fields[$i]->required . ' ' . '' . $response->module_fields[$i]->options . '
And the following is a simple example of how to use the above class to create a simple contact record with a name and an e-mail address, and store it in Sugar's Contacts database.
login(); // populate the contact array $sugar->setContactField('last_name', 'Bloke2'); $sugar->setContactField('email1', 'xyz@wxy.net'); // var_dump($sugar->contact); $sugar->storeContact(); // to Contacts database ?>
To find out all the possible fields in the Contacts database, use this script:
login(); // look what modules sugar exposes $sugar->getModules(); // look in more detail at the fields in a module $sugar->getModuleFields('Accounts'); ?>
Many thanks to this post[1] for getting me on my way.
[1] http://systemsconsciousness.com/2009/04/10/sugarcrm-soap-examples/
posted at: 03:14 | path: /Coding/php | permanent link to this entry