CRM Open Source Business & Social CRM Software

Results 1 to 6 of 6

Thread: Complete SOAP Example?

  1. #1
    jhelms is offline Member
    Join Date
    Jul 2007
    Posts
    8

    Default Complete SOAP Example?

    I am learning SOAP and none of the examples on the WIKI show a complete example. A simple example to retrieve some basic contact information to a form and then allow the user to update it would help me to understand how to use SOAP. Are there any examples to show this?

    Jerry

  2. #2
    thierry.beeckmans is offline Sugar Community Member
    Join Date
    Feb 2007
    Posts
    55

    Default Re: Complete SOAP Example?

    There are examples, but hard to find
    This one you prob. have seen: http://www.sugarcrm.com/wiki/index.p...tical_Examples
    Start here: Simple SOAP script for SugarCRM 4.5

    Beanizer has an older tutorial:
    http://www.beanizer.org/index.php3?page=sugar1
    http://www.beanizer.org/index.php3?page=sugar2

    There's an excellent video session available at:
    http://media.sugarcrm.com/SDUG/2007-...outs-Large.wmv

    And in case you are trying to get data from custom fields:
    http://www.sugarcrm.com/forums/showt...1357#post71357

    I'll give you a few things from my code, but you'll have to adjust it, it's still in development phase
    PHP Code:
        function _connect() {
            if (
    is_null($this->_conn)) {
                
    $this->_soap = @new soapclient($this->_config['helpdesk_soap_url'] . '?wsdl',true);
                
                if (
    $err $this->_soap->getError()) {
                    
    array_push($this->_error$err);
                    return 
    false;
                }
                
                
    $this->_conn $this->_soap->getProxy();
                
                if (
    $err $this->_soap->getError()) {
                    
    array_push($this->_error$err);
                    return 
    false;
                }
            }
            
            return 
    $this->_conn;
        }
        
        function 
    _name_value_pair_to_simple_array($aValuePair){
            
    $aSimpleArray=array();
            while(list(
    $sName,$aValue) = each($aValuePair)){
                
    $aSimpleArray[$aValue['name']] = $aValue['value'];
            }
            
            return 
    $aSimpleArray;
        }
        
        function 
    getError() {
            if (
    count($this->_error)) {
                return 
    $this->_error;
            }
            return 
    false;
        }
        
        function 
    _login() {
            if (
    is_null($this->_sess)) {
                
    $params = array(
                    
    'user_name' => $this->_config['helpdesk_soap_user'],
                    
    'password'  => $this->_config['helpdesk_soap_pass'],
                    
    'version'   => '.01'
                
    );
                
                if (
    $result $this->_request('login',array($params$this->_config['helpdesk_soap_portal']))) {
                    
    $this->_sess $result['id'];
                } else {
                    return 
    false;
                }
            }
            
            return 
    $this->_sess;
        }
        
        
        function 
    _request($sAction$aParams){
            if (
    $this->_connect()) {
                
    $aResult $this->_conn->call($sAction$aParams);
                
                
    // check for errors
                
    if ($err $this->_soap->getError()) {
                    
    array_push($this->_error$err);
                    return 
    false;
                }
                
                switch (
    $sAction) {
                    case 
    'get_entry_list':
                        
    $aMyResult = array();
                        if (
    is_array($aResult['entry_list'])) {
                            foreach (
    $aResult['entry_list'] as $iKey => $aArray) {
                                
    $aMyResult[$iKey] = $this->_name_value_pair_to_simple_array($aArray['name_value_list']);
                            }
                        }
                        return 
    $aMyResult;
                        break;
                    case 
    'get_relationships':
                        return 
    $aResult['ids'];
                        break;
                    default:
                        break;
                }
                
                return 
    $aResult;
            }
            
            return 
    false;
        }
        
        function 
    set_contact($helpdesk_info=null$contact_id=null) {
            if (!
    is_null($this->_login()) && !is_null($helpdesk_info)) {

                
    $set_fields_params = array();
                if (!
    is_null($contact_id)) $set_fields_params[] = array('name' => 'id''value' => $contact_id);
                
    $set_fields_params[] = array('name' => 'email1''value' => $helpdesk_info['email1']);
                
    $set_fields_params[] = array('name' => 'last_name''value' => $helpdesk_info['last_name']);
                
    $set_fields_params[] = array('name' => 'first_name''value' => $helpdesk_info['first_name']);
                
    $set_fields_params[] = array('name' => 'salutation''value' => $helpdesk_info['salutation']);
                
    $set_fields_params[] = array('name' => 'description''value' => $helpdesk_info['description']);
                
    $set_fields_params[] = array('name' => 'phone_work''value' => $helpdesk_info['phone_work']);
    //            $set_fields_params[] = array('name' => 'date_entered', 'value' => date('Y-m-d H:i:s', $helpdesk_info['date_entered']));
    //            $set_fields_params[] = array('name' => 'date_modified', 'value' => date('Y-m-d H:i:s', $helpdesk_info['date_modified']));
                
    $set_fields_params[] = array('name' => 'portal_name''value' => $helpdesk_info['portal_name']);
                
    $set_fields_params[] = array('name' => 'portal_active''value' => 1);
                
                
    $params = array(
                    
    'session' => $this->_sess,
                    
    'module_name' => 'Contacts',
                    
    'name_value_list' => $set_fields_params
                
    );
                
                if (
    $result $this->_request('set_entry',$params)) {
                    return 
    $result['id'];
                }
            }
            
            return 
    false;
        }
        function 
    get_contacts($where=''$maxnum=10$offset=0$orderby=' contacts.last_name ASC') {
            if (!
    is_null($this->_login())) {
                
    $params = array(
                    
    $this->_sess,
                    
    'Contacts',
                    
    $where,
                    
    $orderby,
                    
    $offset,
                    array(
                        
    'id',
                        
    'first_name',
                        
    'last_name',
                        
    'salutation',
                        
    'account_name',
                        
    'account_id',
                        
    'email1',
                        
    'phone_work',
                        
    'portal_name',
                        
    'date_entered',
                        
    'date_modified'
                    
    ),
                    
    $maxnum,
                    
    false
                
    );
                
                if (
    $result $this->_request('get_entry_list',$params)) {
                    if (
    $maxnum == 1) {
                        return 
    $result[0];
                    } else {
                        return 
    $result;
                    }
                }
            }
            
            return 
    false;
        }
        
        function 
    get_cases($where=''$maxnum=10$offset=0$orderby=' cases.date_modified DESC') {
            if (!
    is_null($this->_login())) {
                
    $params = array(
                    
    $this->_sess,
                    
    'Cases',
                    
    $where,
                    
    $orderby,
                    
    $offset,
                    array(
                        
    'id',
                        
    'case_number',
                        
    'name',
                        
    'status',
                        
    'date_modified'
                    
    ),
                    
    $maxnum,
                    
    false
                
    );
                
    $result $this->_request('get_entry_list',$params);
                
                return 
    $result;
            }
        } 

  3. #3
    andydreisch's Avatar
    andydreisch is offline Sugar Team Member
    Join Date
    Apr 2005
    Location
    San Jose
    Posts
    2,080

    Default Re: Complete SOAP Example?

    Hi thierry.beeckmans, thanks for the beanizer links. I've not seen them before.

    Andy
    Andy Dreisch
    Vice President, Online Team


    Check out our Podcasts!
    Sugar University for training
    Sugar Wiki for developer and user help
    SugarForge for modules, themes, lang packs
    SugarExchange for production-ready extensions
    Enter/view bugs via the Sugar bug tracker

  4. #4
    jhelms is offline Member
    Join Date
    Jul 2007
    Posts
    8

    Default Re: Complete SOAP Example?

    Thanks for providing the code samples. My code to connect to Soap does not include the get_proxy method and my code works fine. Does the get_proxy do something that I am missing?

  5. #5
    josh.sweeney is offline Sugar Community Member
    Join Date
    Apr 2008
    Location
    Ga
    Posts
    338

    Default Re: Complete SOAP Example?

    These are all great nusoap examples. Does anyone have good php soap examples that they can post?

  6. #6
    Qamrun is offline Senior Member
    Join Date
    Mar 2009
    Location
    New Delhi
    Posts
    46

    Default Re: Complete SOAP Example?

    SugarSoap.php

    PHP Code:
    <?php
    //path to nusoap
    require_once("Sugar/include/nusoap/nusoap.php");

    class 
    SugarSoap{
    var 
    $proxy;
    var 
    $sess;
    function 
    SugarSoap($soap_url,$login=true){
    $soapclient = new nusoapclient($soap_url,true);
    $this->proxy $soapclient->getProxy();
    if(
    $login$this->login();
    }

    function 
    login(){
    $params = array(
    'user_name' => 'username',
    'password' => md5('password'),
    );
    $result $this->proxy->login($params,'test');
    $this->sess$result['error']['number']==$result['id'] : null;
    return 
    $this->sess;
    }

    function 
    getContacts($query='',$maxnum=0,$orderby='',$modul e_name,$fields_array){
    $result $this->proxy->get_entry_list($this->sess,
    $module_name,
    $query,
    $orderby,
    0,
    $fields_array,
    $maxnum,
    false
    );
    return 
    $result;
    }

    function 
    nameValuePairToSimpleArray($array){
    $my_array=array();
    while(list(
    $name,$value)=each($array)){
    $my_array[$value['name']]=$value['value'];
    }
    return 
    $my_array;
    }
    }
    ?>
    Testing soap:

    test_soap.php


    PHP Code:
    <?php
        
    if(!defined('sugarEntry'))define('sugarEntry'true); 
         require_once 
    "SugarSoap.php";
         
    $soap=new SugarSoap('http://mysugarsite/soap.php?wsdl'); // we automatically log in
         
    $result=$soap->getLeads(" leads.first_name like 'a%' ",0,' leads.last_name desc','Leads',array('id','first_name','last_name'));
        
    //echo "<pre>";
         //print_r($result);
         //echo $result['result_count'];
         
    if($result['result_count']>0){ 
             foreach(
    $result['entry_list'] as $record){
                
    $array$soap->nameValuePairToSimpleArray($record['name_value_list']);
                echo 
    $array['first_name'] . " " $array['last_name']. "<br>";                      }
         } else {
             echo 
    "No leads found";
         }
     
    ?>
    Last edited by Qamrun; 2009-04-28 at 09:49 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. SOAP login() - Invalid Username/Password.
    By dsandor in forum Developer Help
    Replies: 12
    Last Post: 2010-12-07, 10:13 AM
  2. Soap, Sugar and Python.
    By giblfiz in forum Developer Help
    Replies: 2
    Last Post: 2009-03-24, 10:53 PM
  3. SOAP Performance Issue Loading Accounts
    By artisticlight in forum General Discussion
    Replies: 1
    Last Post: 2007-07-20, 02:39 PM
  4. Soap & New Custom Fields
    By sacramentojoe in forum Developer Help
    Replies: 0
    Last Post: 2007-04-17, 05:04 PM
  5. SOAP and Applescript?
    By mattjones99 in forum Developer Help
    Replies: 5
    Last Post: 2006-02-08, 09:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •