I recently developed a connector script for using the OTRS API Generic Interface with PHP/SOAP. Feel free to use any of the code below for your own project.
You will have to create a WebService in OTRS. Once You have set it up, you can use PHP SOAP functions.
Code: Select all
<?php
namespace ClusterNetCore\Modules\App\API\External\OTRSConnector;
class OTRSConnectorMain extends \ClusterNetCore\Modules\Module\ModuleMain{
protected $SOAPClientInstance;
protected $LoggedIn = false;
protected $Credentials = array();
protected $GBSNetCredentials = array();
protected $OTRSSpecialCredentials = array();
public $SupportTickets = array();
public $SupportTicketArticles = array();
public $FAQCategories = array();
public $FAQArticles = array();
protected $SupportTicketsLoaded = false;
protected $SupportTicketArticlesLoaded = false;
protected $FAQCategoriesLoaded = false;
protected $FAQArticlesLoaded = false;
protected $HasUserData = true;
protected $AdditionalFields = array(
'Location' => 4,
'Raumnummer' => 3,
'Contact' => 5
);
public function ModuleConstructor($Parameters)
{
//https://yourhost.com/otrs/nph-genericinterface.pl
$URL = $this->App->CredentialManagerInstance->GetFieldModuleInstance('App_ExternalApplication_Key')->GetExternalApplicationURL('OTRS', 'SOAP');
$this->Credentials = $this->App->CredentialManagerInstance->GetExternalApplicationCredentials('OTRS');
$this->GBSNetCredentials = $this->App->CredentialManagerInstance->GetExternalApplicationCredentials('GBSNet');
$SessionCookie = array();
$this->LoggedIn = true;
$WebserviceName = $this->Configuration['WebserviceName'];
try
{
$this->SOAPClientInstance = new \SoapClient(null, array(
'location'=> $URL.'/Webservice/'.$WebserviceName.'/',
'uri' => $this->Configuration['Namespace'],
'trace' => 1,
//'login' => $this->GBSNetCredentials['Username'],
//'password'=> $this->GBSNetCredentials['Password'],
'login' => $this->Configuration['SOAPUserCredentials']['Username'],
'password'=> $this->Configuration['SOAPUserCredentials']['Password'],
'style' => SOAP_RPC,
'use' => SOAP_ENCODED
)
);
}
catch (\Exception $e) {
echo "Exception Error: ",$e->getMessage(), "\n";
}
}
public function LoadSupportTickets($FilterOptions = array(), $ReturnIDList = false)
{
if(!$this->SupportTicketsLoaded)
{
$time1 = microtime(1);
if(isset($FilterOptions['StateType']))
{
$StateTypes = $FilterOptions['StateType'];
}
else
{
$StateTypes = array('Open', 'New', 'Closed', 'pending auto close+');
}
$TicketSearchOptions = array(
new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
new \SoapParam($this->Configuration['SOAPUserCredentials']['Username'], 'UserLogin'),
new \SoapParam('ro', 'Permission'),
new \SoapParam('ARRAY', 'Result'),
new \SoapParam($StateTypes, 'StateType'),
new \SoapParam('Changed', 'SortBy'),
);
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".print_r($TicketSearchOptions, true));
}
try
{
$Result = $this->SOAPClientInstance->__soapCall("TicketSearch",
$TicketSearchOptions
);
}
catch(\Exception $e)
{
echo "Exception Error: ",$e->getMessage(), "\n";
print $this->SOAPClientInstance->__getLastRequest();
print $this->SOAPClientInstance->__getLastResponse();
}
if(is_string($Result))
{
$TicketID = $Result;
$Result = array(
'TicketID' => array($TicketID),
);
}
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
}
if($ReturnIDList === true)
{
return $Result['TicketID'];
}
else
{
$this->LoadSupportTicketsByID($Result['TicketID']);
$this->SupportTicketsLoaded = true;
}
}
}
public function LoadSupportTicketsByID($IDList = array(), $LoadArticles = false)
{
if(!empty($IDList))
{
$time1 = microtime(1);
$TicketRequestArray = array(
new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
new \SoapParam($IDList, 'TicketID'),
new \SoapParam(1, 'DynamicFields'),
new \SoapParam((int)$LoadArticles, 'AllArticles'),
);
$Result = $this->SOAPClientInstance->__soapCall("TicketGet",
$TicketRequestArray
);
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time3: '.(microtime(1)-$time1));
}
if(is_object($Result))
{
$Result = array('Ticket' => array($Result));
}
if(!is_array($Result))
{
return array();
}
foreach($Result['Ticket'] as $RowObject)
{
$RowObject->TicketNumber = number_format($RowObject->TicketNumber, 0, '.','');
$this->SupportTickets[] = (array)$RowObject;
}
}
}
public function SaveSupportTicket($TicketData, $ArticleData = array(), $Attachments = array())
{
$DefaultTicketData = array(
'TicketID' => 0,
'Title'=> 't',
'From'=> $this->Credentials['Username'],
'Queue'=> '.SPOC',
'Lock' => 'unlock',
'Priority' => '3 normal',
'State'=> 'new',
'CustomerUser' => $this->Credentials['Username'],
'OwnerID' => 1,
'Location' => '',
'Raumnummer' => '',
'Contact' => '',
);
$DefaultArticleData = array(
'From' => $this->GBSNetCredentials['Username'],
'Subject' => '',
'Body' => '',
'MimeType' => 'text/html',
'Charset'=> 'utf8',
);
$DefaultAttachmentData = array(
'Content' => '',
'ContentType' => 'application/download; charset=utf8',
'Filename' => '',
);
$TicketData = array_merge($DefaultTicketData, $TicketData);
$ArticleData = array_merge($DefaultArticleData, $ArticleData);
$AttachmentsToAdd = array();
foreach($Attachments as &$Attachment)
{
$Attachment = array_merge($DefaultAttachmentData, $Attachment);
if(!empty($Attachment['Filename']))
{
$AttachmentsToAdd[] = &$Attachment;
}
}
$TicketData['CustomerUser'] = $this->Credentials['Username'];
$TicketDynamicFieldRequestArray = array(
);
foreach($this->AdditionalFields as $Key => $FieldID)
{
if(isset($TicketData['DynamicField_'.$Key]) && !empty($TicketData['DynamicField_'.$Key]))
{
$TicketDynamicFieldRequestArray[] = array(
'Name' => $Key,
'Value' => $TicketData['DynamicField_'.$Key],
);
}
}
$time1 = microtime(1);
$TicketRequestArray = array(
new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
new \SoapParam($this->Configuration['SOAPUserCredentials']['Username'], 'UserLogin'),
new \SoapParam($this->Credentials['Password'], "Password"),
);
if($TicketData['TicketID']>0)
{
$TicketID = $TicketData['TicketID'];
$TicketRequestArray[] = new \SoapParam($TicketID, 'TicketID');
$TicketRequestArray[] = new \SoapParam(array(
'State' => 'open'
), 'Ticket');
$Action = 'TicketUpdate';
}
else
{
$Action = 'TicketCreate';
$TicketRequestArray[] = new \SoapParam($TicketDynamicFieldRequestArray, 'DynamicField');
$TicketRequestArray[] = new \SoapParam($TicketData, 'Ticket');
if(empty($ArticleData['Body']))
{
die();
}
}
if(!empty($ArticleData['Body']))
{
$TicketRequestArray[] = new \SoapParam($ArticleData, 'Article');
}
if(!empty($AttachmentsToAdd))
{
$TicketRequestArray[] = new \SoapParam($AttachmentsToAdd, 'Attachment');
}
$Response = $this->SOAPClientInstance->__soapCall($Action,
$TicketRequestArray
);
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
}
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".print_r($Response, true));
}
return (array)$Response;
}
public function LoadFAQCategories()
{
if(!$this->FAQCategoriesLoaded)
{
$time1 = microtime(1);
$RequestOptions = array(
new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
);
try
{
$Result = $this->SOAPClientInstance->__soapCall("FAQPublicCategoryList",
$RequestOptions
);
}
catch(\Exception $e)
{
echo "Exception Error: ",$e->getMessage(), "\n";
print $this->SOAPClientInstance->__getLastRequest();
print $this->SOAPClientInstance->__getLastResponse();
}
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
}
if(isset($Result['Category']))
{
foreach($Result['Category'] as &$Category)
{
$Category = (array)$Category;
}
$this->FAQCategories = $Result['Category'];
$this->FAQCategoriesLoaded = true;
}
}
}
public function LoadFAQArticles($FilterOptions = array(), $ReturnIDList = false, $NoAttachments = true)
{
if(!$this->FAQArticlesLoaded)
{
$time1 = microtime(1);
$TicketSearchOptions = array(
new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
);
if(isset($FilterOptions['CategoryID']))
{
if(!is_array($FilterOptions['CategoryID']))
{
$FilterOptions['CategoryID'] = array($FilterOptions['CategoryID']);
}
$TicketSearchOptions[] = new \SoapParam($FilterOptions['CategoryID'], 'CategoryID');
}
if(isset($FilterOptions['SearchKeywords']))
{
$TicketSearchOptions[] = new \SoapParam('*'.implode(' ', $FilterOptions['SearchKeywords']).'*', 'What');
}
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".print_r($TicketSearchOptions, true));
}
try
{
$Result = $this->SOAPClientInstance->__soapCall("FAQPublicFAQSearch",
$TicketSearchOptions
);
}
catch(\Exception $e)
{
echo "Exception Error: ",$e->getMessage(), "\n";
print $this->SOAPClientInstance->__getLastRequest();
print $this->SOAPClientInstance->__getLastResponse();
}
if(is_string($Result))
{
$TicketID = $Result;
$Result = array(
'ID' => array($TicketID),
);
}
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time1: '.(microtime(1)-$time1));
}
if($ReturnIDList === true)
{
return $Result['ID'];
}
else
{
$this->LoadFAQArticlesByID($Result['ID'], $NoAttachments);
$this->FAQArticlesLoaded = true;
}
}
}
public function LoadFAQArticlesByID($IDList = array(), $NoAttachments = true)
{
if(!empty($IDList))
{
$time1 = microtime(1);
$TicketRequestArray = array(
new \SoapParam($this->Credentials['Username'], 'CustomerUserLogin'),
new \SoapParam($this->Credentials['Password'], "Password"),
new \SoapParam(implode(',',$IDList), 'ItemID'),
new \SoapParam((int)!$NoAttachments, "GetAttachments"),
);
$Result = $this->SOAPClientInstance->__soapCall("FAQPublicFAQGet",
$TicketRequestArray
);
if($this->App->Debug)
{
$this->App->DebugInfo($this->UniqueModulePath.":\n".$this->UniqueModulePath.":\n".'time3: '.(microtime(1)-$time1));
}
if(is_object($Result))
{
$Result = array('FAQItem' => array($Result));
}
if(!is_array($Result))
{
return array();
}
foreach($Result['FAQItem'] as $Key=>$RowObject)
{
$this->FAQArticles[] = (array)$RowObject;
}
}
}
}
?>
macjohnny