Darcy, try this;
Not sure how good my coding is, but I noticed getting a time-out after leaving for a while, so set a 15 minute check to log in. This means that within 15 minutes, you can use the same id captured in a session. I thought it also better than creating a new session each time the app connected to do something...
Below that I put the code to make a relationship.
Note: $soapclient->decodeUTF8(false); deals with multi-byte charcters and allows being returned as UTF-8 because SugarCRM for some reason send it as ASCII - which turns into "mojibake" with Japanese.
PHP Code:
# Start Here
##############################################
# Setting SOAP Access
function do_stuff ($api_user, $api_pass, $crm_wsdl_url, $action, $params){
// Set up session with timeout check
$sugar_sessioner = $_SESSION["sugarsoapsessionid"];
$date = date("Y-m-d G:i:s");
$auth_array = array( 'user_auth' => array ('user_name' => $api_user,
'password' => md5($api_pass),
)
);
require_once ("nusoap/nusoap.php");
$soapclient = new nusoap_client( $crm_wsdl_url, true );
$soapclient->decodeUTF8(false);
if ($sugar_sessioner == NULL){
$login_results = $soapclient->call('login',$auth_array);
$sugar_session_id = $login_results['id'];
$sugar_sessioner = $sugar_session_id."[]".$date;
$_SESSION["sugarsoapsessionid"] = $sugar_sessioner;
} else {
// echo "SESSIONER: ".$sugar_sessioner."<BR>";
// Session will expire - give ourselves time limit of say 15 mins
$thisminutes = date("i");
list($sugar_session_id,$createdtime) = explode('[]',$sugar_sessioner);
// Must check session timeout
if ($createdtime == NULL){
$checktime = 100;
} else {
list ($datep1, $datep2) = explode (" ", $createdtime);
list ($hours, $minutes, $seconds) = explode (":", $datep2);
$checktime = $minutes+15;
if ($checktime>60){
$checktime = $checktime-60;
}
} // end if not null
if (($checktime>($thisminutes+14)) || (($thisminutes-16)<$checktime)){
$login_results = $soapclient->call('login',$auth_array);
$sugar_session_id = $login_results['id'];
$sugar_sessioner = $sugar_session_id."[]".$date;
$_SESSION["sugarsoapsessionid"] = $sugar_sessioner;
} else {
// All sweet
list($sugar_session_id, $createdtime) = explode('[]',$sugar_sessioner);
// echo "SESS: ".$sugar_session_id."<BR>";
}
} // end if has session
# End setting SOAP Access
##############################################
# Relate IDs in two Modules
switch ($action){
case 'relatemymodules':
$set_rel_params = array(
'session' => $sugar_session_id,
'set_relationship_value'=>array(
'module1'=> $params[0],
'module1_id'=> $params[1],
'module2' => $params[2],
'module2_id'=> $params[3]
)
);
// Now Add the Product
$returnpack = $soapclient->call('set_relationship',$set_rel_params);
break;
} // end switch
} // end function
# EndRelate IDs in two Modules
##############################################
# Set up the params to send to this;
$api_user = "admin";
$api_pass = "mypass";
$crm_wsdl_url = "http://www.mydonain.com/sugar/soap.php?wsdl";
$params[0] = "Accounts";
$params[1] = "92e05b85-4526-c287-cc2d-496efd9f0b18";
$params[2] = "Contacts";
$params[3] = "64f06b83-4234-e456-df6q-867ewe3f1t98";
$action = "relatemymodules";
$returned = do_stuff ($api_user, $api_pass, $crm_wsdl_url, $action, $params);
var_dump ($returned);
# End app
###############################################
I think that should work...good luck!
Bookmarks